tuomasvaltanen

Untitled

Oct 4th, 2021 (edited)
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.39 KB | None | 0 0
  1. # Adobe Connect 4.10.2021, string manipulation and error handling
  2. print("Welcome!")
  3.  
  4. text = input("Give some text:\n")
  5.  
  6. print(text)
  7.  
  8. # slice from beginning
  9. substring = text[0:8]
  10. print(substring)
  11.  
  12. # slice from middle of sentence
  13. substring2 = text[3:10]
  14. print(substring2)
  15.  
  16. # NEW FILE
  17.  
  18. # removing an extra character from the end
  19. # we can use a negative approach
  20. # -1 => second last character
  21. years = "2021-2020-2019-2018-"
  22.  
  23. years = years[0:-1]
  24. print(years)
  25.  
  26. # NEW FILE
  27.  
  28. text = input("Give some text!\n")
  29.  
  30. # getting the amonut of characters in a string
  31. text_length = len(text)
  32.  
  33. print(f"Your text has {text_length} characters!")
  34. print()
  35.  
  36. # check if your text is empty
  37. # you can also compare to ''
  38. if text_length == 0:
  39.     print("YOUR TEXT IS EMPTY.")
  40.  
  41. # if/else, short or long text?
  42. if text_length > 10:
  43.     print("That's a long piece of text!")
  44. else:
  45.     print("That text is a bit short, isn't it?")
  46.  
  47. # VERSION 2:
  48.  
  49. # check if your text is empty
  50. # you can also compare to ''
  51. if text_length == 0:
  52.     print("YOUR TEXT IS EMPTY.")
  53. else:
  54.     # if/else, short or long text?
  55.     if text_length > 10:
  56.         print("That's a long piece of text!")
  57.     else:
  58.         print("That text is a bit short, isn't it?")
  59.  
  60. # NEW FILE
  61.  
  62. text = input("Give some text!\n")
  63.  
  64. # force all letters to lowercase
  65. text = text.lower()
  66. print(text)
  67.  
  68. # count a's and o's in sentence
  69. a_letters = text.count("a")
  70. o_letters = text.count("o")
  71.  
  72. total = a_letters + o_letters
  73.  
  74. # print out the results
  75. print(f"Your sentence has {a_letters} a-letters!")
  76. print(f"Your sentence has {o_letters} o-letters!")
  77.  
  78. # reverse the text
  79. reversed_text = text[::-1]
  80. print(reversed_text)
  81.  
  82. # NEW FILE
  83.  
  84. # replacing a word
  85. drinks = "water, milk, tea, coffee, soda"
  86. new_text = drinks.replace("soda", "juice")
  87. print(new_text)
  88.  
  89. # replace replaces all occurences by default
  90. drinks = "water, milk, water, coffee, tea"
  91. new_text = drinks.replace("water", "soda")
  92. print(new_text)
  93.  
  94. # replace only one
  95. drinks = "water, milk, water, coffee, tea"
  96. new_text = drinks.replace("water", "soda", 1)
  97. print(new_text)
  98.  
  99. # this is also possible
  100. new_text = new_text.replace(" ", "\n")
  101. print(new_text)
  102.  
  103. # NEW FILE
  104.  
  105. drinks = "water, milk, coffee, tea"
  106.  
  107. choice = input("What would you like to drink?\n")
  108.  
  109. # check if users word is in the text
  110. if choice in drinks:
  111.     print("Word found!")
  112. else:
  113.     print("Word not found...")
  114.  
  115.  
  116. # NEW FILE
  117.  
  118. text = input("Give text or number:\n")
  119.  
  120. # we can react with an if-statement
  121. # if user gave text or a number!
  122. # => your application can work differently
  123. # depending on this!
  124. if text.isnumeric():
  125.     print("User gave you a number!")
  126.     number = int(text)
  127.  
  128.     number = number + 100
  129.     print(number)
  130.  
  131. else:
  132.     print("User gave you text.")
  133.  
  134. # NEW FILE
  135.  
  136. try:
  137.     number = input("Give a number:\n")
  138.     number = int(number)
  139.  
  140.     number = number + 10
  141.     print(number)
  142.  
  143.     print("This is my application!")
  144.  
  145. except ValueError:
  146.     print("You wrote text! Please give a number.")
  147.  
  148. # NEW FILE
  149.  
  150. try:
  151.     # ask two numbers from user
  152.     number1 = input("Give a number:\n")
  153.     number1 = int(number1)
  154.  
  155.     number2 = input("Give another number:\n")
  156.     number2 = int(number2)
  157.  
  158.     # division
  159.     number = number1 / number2
  160.     print(number)
  161.  
  162.     print("This is my application!")
  163.  
  164. except ValueError:
  165.     # user wrote text
  166.     print("You wrote text! Please give a number.")
  167. except ZeroDivisionError:
  168.     # seconds number was zero => divided by zero
  169.     print("You divided by zero!")
  170. except Exception as e:
  171.     # something else happened...error anyways.
  172.     print(str(e))
  173.     print("There was an error.")
  174.  
  175. # application doesn't crash and this is printed too!
  176. print("Thanks for using the application!")
  177.  
  178. # NEW FILE
  179.  
  180. # example, a client id follows this format: C1324_4356
  181.  
  182. try:
  183.     client = input("Give a client id:")
  184.  
  185.     # the length of the client id should be 10 characters
  186.     text_length = len(client)
  187.  
  188.     # client id should be 10 characters long
  189.     # and 6th character should be an underscore!
  190.     if text_length != 10:
  191.         print("Client id length not correct.")
  192.     elif client[5] != "_":
  193.         print("Underscore is missing!")
  194.     else:
  195.         # everything seems to be okay
  196.         id = client[0:5]
  197.         order = client[6:10]
  198.         order = int(order)
  199.  
  200.         print(id)
  201.         print(order)
  202.  
  203. except Exception as e:
  204.     print("Error: " + str(e))
Add Comment
Please, Sign In to add comment