Advertisement
tuomasvaltanen

Untitled

Oct 7th, 2022 (edited)
1,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.40 KB | None | 0 0
  1. # string manipulation and error handling today!
  2. print("Welcome!")
  3.  
  4. # NEW FILE
  5.  
  6. # we can alternate quotation marks and single quotes
  7. # to literally print out quotation marks too
  8. name = 'John "Supercoder" Doe'
  9. print(name)
  10.  
  11. # escaping the quotation marks works too
  12. # => basically we're marking down quotation marks exactly in our string
  13. name = "John \"Supercoder\" Doe"
  14. print(name)
  15.  
  16. # NEW FILE
  17.  
  18. text = "Rovaniemi is a city near Arctic Circle, and it tends be rather chilly in January."
  19. print(text)
  20.  
  21. # substring, partial text => from first character to 25th
  22. # first character is at position 0, second in position 1 etc.
  23. subtext = text[0:25]
  24. print(subtext)
  25.  
  26. # take a substring from the middle
  27. # make sure you're not trying to slice positions
  28. # that do not exist (for example if string is too short()
  29. subtext = text[10:30]
  30. print(subtext)
  31.  
  32. # get last 10 characters from string
  33. subtext = text[-10:]
  34. print(subtext)
  35.  
  36. # take out the last character
  37. # in this case, the extra dash (-) we have
  38. years = "2022-2021-2020-2019-2018-"
  39. years = years [0:-1]
  40.  
  41. print(years)
  42.  
  43. # NEW FILE
  44.  
  45. text = input("Please write something:\n")
  46.  
  47. # how long is this text? whitespaces also count!
  48. text_length = len(text)
  49. print(text_length)
  50.  
  51. # check if user actually gave any text
  52. if text_length == 0:
  53.     print("User didn't input any text!")
  54. else:
  55.     # user gave some text! is the text long or not?
  56.     if text_length > 30:
  57.         print("Long text!")
  58.     else:
  59.         print("Short text...")
  60.  
  61. # NEW FILE
  62.  
  63. text = "The autumn colors seem to be quite bright this time of year."
  64. print(text)
  65.  
  66. # how many letter a in this string?
  67. # note: capital letters are different letters than lowercase
  68. a_letters = text.count("a")
  69. print(f"Amount of letter a: {a_letters}")
  70.  
  71. # for example: the capital T in the beginning
  72. # doesn't count!
  73. t_letters = text.count("t")
  74. print(f"Amount of letter a: {t_letters}")
  75.  
  76. # NEW FILE
  77.  
  78. text = input("Write something:\n")
  79.  
  80. # reverse the text
  81. reversed_text = text[::-1]
  82.  
  83. # for a few palindromes, try tacocat and racecar
  84. # palindrome = text is exactly same even if reversed
  85. print(reversed_text)
  86.  
  87. # NEW FILE
  88.  
  89. drinks = "soda, milk, coffee, tea, soda, water"
  90.  
  91. # replace all soda with juice
  92. # try also drinks.replace("soda", "juice", 1) in order to
  93. # replace only the first soda in the text!
  94. new_drinks = drinks.replace("soda", "juice")
  95. print(new_drinks)
  96. print()
  97.  
  98. # commas to newlines!
  99. new_drinks2 = new_drinks.replace(", ", "\n")
  100. print(new_drinks2)
  101. print()
  102.  
  103. # check if a string is inside a string
  104. text = input("What would you like to drink?\n")
  105.  
  106. # check if text inside drinks
  107. if text in drinks:
  108.     print("Found it!")
  109. else:
  110.     print("That drink not available :(")
  111.  
  112. # NEW FILE
  113.  
  114. text = input("Give a number?\n")
  115.  
  116. # check if user actually gave a number or text
  117. if text.isnumeric():
  118.     print("User gave a number!")
  119.  
  120.     # we can now trust the user actually gave a number
  121.     number = int(text)
  122.     number = number * 2
  123.     print(number)
  124. else:
  125.     print("User gave text!")
  126.  
  127. # NEW FILE
  128.  
  129. text = input("Are you student or adult? (a/s)\n")
  130.  
  131. # convert the user input to lowercase, ALWAYS
  132. # without this, the if-statements would have to be something like
  133. # if text == "a" or text == "A": etc
  134. text = text.lower()
  135.  
  136. # check the result
  137. if text == "a":
  138.     print("Adult!")
  139. elif text == "s":
  140.     print("Student!")
  141. else:
  142.     print("Incorrect selection.")
  143.  
  144. # NEW FILE
  145.  
  146. # we have some code in try-block
  147. # that might crash (converting text into integer)
  148. # what if user writes text instead?
  149. try:
  150.     number = input("Give a number:\n")
  151.     number = int(number)
  152.     print(number)
  153. except ValueError:
  154.     # handle the error message!
  155.     print("Please write a number instead.")
  156.  
  157. # NEW FILE
  158.  
  159. # we have some code in try-block
  160. # that might crash (converting text into integer)
  161. # what if user writes text instead?
  162. try:
  163.     number = input("Give a number:\n")
  164.     number = int(number)
  165.     result = 100 / number
  166.     print(f"Number: {number}, result: {result}")
  167. except ValueError:
  168.     # handle the error message!
  169.     print("Please write a number instead.")
  170. except ZeroDivisionError:
  171.     print("You can't divide by zero!")
  172.  
  173. # NEW FILE
  174.  
  175. # we have some code in try-block
  176. # that might crash (converting text into integer)
  177. # what if user writes text instead?
  178. # you can also catch all errors with the generic
  179. # except statement
  180. try:
  181.     number = input("Give a number:\n")
  182.     number = int(number)
  183.     result = 100 / number
  184.     print(f"Number: {number}, result: {result}")
  185. except Exception as e:
  186.     print("Error: " + str(e))
  187.  
  188. # NEW FILE
  189.  
  190. # an example: we can have a client id following this format: C1234_4567
  191. # the logic:
  192. # exactly 10 characters long, sixth character has to be underscore
  193. # last for characters has to be an integer
  194.  
  195. try:
  196.     client = input("Give client id:\n")
  197.    
  198.     # get client id length
  199.     text_length = len(client)
  200.  
  201.     # the client id should be exactly 10 characters long
  202.     # sixth character should be an underscore
  203.     if text_length != 10:
  204.         print("Incorrect length for client id.")
  205.     elif client[5] != "_":
  206.         print("Underscore missing in client id.")
  207.     else:
  208.         print("Seems okay!")
  209.  
  210.         # use substring to get two parts out of client id
  211.         id = client[0:5]
  212.         order = client[6:10]
  213.         order = int(order)
  214.  
  215.         print(id)
  216.         print(order)
  217.  
  218. except Exception as e:
  219.     print("Error: " + str(e))
  220.    
  221.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement