Advertisement
tuomasvaltanen

Untitled

Oct 6th, 2021 (edited)
948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. # Adobe Connect, coding workshop, 6.10.2021
  2. print("Welcome!")
  3.  
  4. # ask number and check if it's numeric or not
  5. number = input("Numbers: (max five):\n")
  6. number = str(number)
  7.  
  8. # this checks only letters and numbers
  9. # spaces are not a letter or a number in this case
  10. if number.isalnum():
  11.     print("Text is only letters and numbers.")
  12. else:
  13.     print("You have something else there as well, don't ya?")
  14.  
  15. # for this purpose, isnumeric() works better!
  16. if number.isnumeric():
  17.     is_number = True
  18. else:
  19.     is_number = False
  20.  
  21. print(is_number)
  22.  
  23. # NEW FILE
  24.  
  25. # THIS IS A NEW VERSION OF THE CLIENT ID EXAMPLE FROM LECTURE 4
  26. # HOW TO PRINT MULTIPLE ERROR MESSAGES AT ONCE AND HOW TO USE A
  27. # BOOLEAN FOR CHECKING INPUT VALIDITY
  28.  
  29. # example, a client id follows this format: C1324_4356
  30.  
  31. try:
  32.     client = input("Give a client id:\n")
  33.  
  34.     valid_id = True
  35.  
  36.     # the length of the client id should be 10 characters
  37.     text_length = len(client)
  38.  
  39.     # client id should be 10 characters long
  40.     # and 6th character should be an underscore!
  41.     if text_length != 10:
  42.         print("Client id length not correct.")
  43.         valid_id = False
  44.  
  45.     # the first letter here should be C
  46.     # first check if text is not empty before checking first letter
  47.     # otherwise it will crash
  48.     if text_length > 0:
  49.         if client[0] != "C":
  50.             print("Client id should start with 'C'.")
  51.             valid_id = False
  52.  
  53.     # only check underscore if string is at least 6 characters so
  54.     # that application doesn't crash
  55.     if text_length >= 6:
  56.         if client[5] != "_":
  57.             print("Underscore is missing!")
  58.             valid_id = False
  59.  
  60.     # only print the information if the id was in valid format!
  61.     if valid_id:
  62.         # everything seems to be okay
  63.         id = client[0:5]
  64.         order = client[6:10]
  65.         order = int(order)
  66.  
  67.         print(id)
  68.         print(order)
  69.  
  70. except Exception as e:
  71.     print("Error: " + str(e))
  72.  
  73. # NEW FILE
  74.  
  75. # Example: ask the user two different
  76. # numbers and then multiple
  77. # them with each other and round to third decimal.
  78.  
  79. # try this with some decimal numbers, for example
  80. # 15.8756 and 14.6713 or something like that
  81.  
  82. number1 = input("Give number 1:\n")
  83. number1 = float(number1)
  84.  
  85. number2 = input("Give number 2:\n")
  86. number2 = float(number2)
  87.  
  88. # do the calculation
  89. result = number1 * number2
  90.  
  91. # only round just before printing out
  92. # avoid intermediate roundings!
  93. # otherwise you might lose precision in calculations
  94. # and the results won't be 100% correct
  95. result = round(result, 3)
  96. print(f"The result is: {result}")
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement