Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Adobe Connect, coding workshop, 6.10.2021
- print("Welcome!")
- # ask number and check if it's numeric or not
- number = input("Numbers: (max five):\n")
- number = str(number)
- # this checks only letters and numbers
- # spaces are not a letter or a number in this case
- if number.isalnum():
- print("Text is only letters and numbers.")
- else:
- print("You have something else there as well, don't ya?")
- # for this purpose, isnumeric() works better!
- if number.isnumeric():
- is_number = True
- else:
- is_number = False
- print(is_number)
- # NEW FILE
- # THIS IS A NEW VERSION OF THE CLIENT ID EXAMPLE FROM LECTURE 4
- # HOW TO PRINT MULTIPLE ERROR MESSAGES AT ONCE AND HOW TO USE A
- # BOOLEAN FOR CHECKING INPUT VALIDITY
- # example, a client id follows this format: C1324_4356
- try:
- client = input("Give a client id:\n")
- valid_id = True
- # the length of the client id should be 10 characters
- text_length = len(client)
- # client id should be 10 characters long
- # and 6th character should be an underscore!
- if text_length != 10:
- print("Client id length not correct.")
- valid_id = False
- # the first letter here should be C
- # first check if text is not empty before checking first letter
- # otherwise it will crash
- if text_length > 0:
- if client[0] != "C":
- print("Client id should start with 'C'.")
- valid_id = False
- # only check underscore if string is at least 6 characters so
- # that application doesn't crash
- if text_length >= 6:
- if client[5] != "_":
- print("Underscore is missing!")
- valid_id = False
- # only print the information if the id was in valid format!
- if valid_id:
- # everything seems to be okay
- id = client[0:5]
- order = client[6:10]
- order = int(order)
- print(id)
- print(order)
- except Exception as e:
- print("Error: " + str(e))
- # NEW FILE
- # Example: ask the user two different
- # numbers and then multiple
- # them with each other and round to third decimal.
- # try this with some decimal numbers, for example
- # 15.8756 and 14.6713 or something like that
- number1 = input("Give number 1:\n")
- number1 = float(number1)
- number2 = input("Give number 2:\n")
- number2 = float(number2)
- # do the calculation
- result = number1 * number2
- # only round just before printing out
- # avoid intermediate roundings!
- # otherwise you might lose precision in calculations
- # and the results won't be 100% correct
- result = round(result, 3)
- print(f"The result is: {result}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement