Advertisement
tuomasvaltanen

Untitled

Sep 17th, 2024 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.08 KB | None | 0 0
  1. # lecture 3, conditional statements!
  2. print("Welcome!")
  3.  
  4. # NEW FILE
  5.  
  6. from decimal import Decimal
  7.  
  8. # this is a somewhat tedious feature of the float data type
  9. number1 = float(0.1)
  10. number2 = float(0.2)
  11. print(f"Normal float/decimal numbers: {number1} + {number2} =")
  12. print(number1 + number2)
  13.  
  14. # 0.1 is a problematic float value, or pretty much all values that are between 0 and 0.1
  15. # this happens because of the method how computers handle floating numbers
  16. # basically this is a problem in each programming languages, however, some programming languages "take care of this"
  17. # automatically for the programmer
  18.  
  19. # easy fix usually: round the value
  20. total = number1 + number2
  21. total = round(total, 1)
  22. print(total)
  23.  
  24. # one way is to use the decimal-module in Python
  25. # note: remember to import decimal
  26. number3 = Decimal("0.1")
  27. number4 = Decimal("0.2")
  28. print(f"Decimal numbers of the decimal-module: {number3} + {number4} =")
  29. print(number3 + number4)
  30.  
  31. # NEW FILE
  32.  
  33. # user's age
  34. age = 19
  35.  
  36. # conditional statement -> is age under 20?
  37. # if it is -> run the print-code below
  38. if age < 20:
  39.     print("You are less than 20 years old.")
  40.     print("This is also only run if the condition is true.")
  41.  
  42. print("This is run regardless of the if-statement, since it has no indentation.")
  43.  
  44. # NEW FILE
  45.  
  46. # ask user's age (integer)
  47. age = input("How old are you?\n")
  48. age = int(age)
  49.  
  50. # is the user less than 20 years old?
  51. # if yes => print "You're less than 20 years old."
  52. # if not => go to the else -statement
  53. if age < 20:
  54.     print("You're less than 20 years old.")
  55. else:
  56.     print("You're at least 20 years old.")
  57.  
  58. print("Thank you for using the application!")
  59.  
  60. # NEW FILE
  61.  
  62. # ask user's age (integer)
  63. age = input("How old are you?\n")
  64. age = int(age)
  65.  
  66. # is the user less than 20 years old?
  67. # if yes => print "You're less than 20 years old."
  68. # if not => go to the else -statement
  69. if age < 20:
  70.     print("You're less than 20 years old.")
  71. elif age < 30:
  72.     print("You're less than 30 years old.")
  73. elif age < 40:
  74.     print("You're less than 40 years old.")
  75. else:
  76.     print("Your age is something else.")
  77.  
  78. print("Thank you for using the application!")
  79.  
  80. # NEW FILE
  81.  
  82. # ask user's age (integer)
  83. age = input("How old are you?\n")
  84. age = int(age)
  85.  
  86. # ask month number
  87. month = input("Which month you wish to visit the store?\n")
  88. month = int(month)
  89.  
  90. # is the user less than 20 years old?
  91. # if yes => print "You're less than 20 years old."
  92. # if not => go to the else -statement
  93. if age < 20:
  94.     print("You're less than 20 years old.")
  95. elif age < 30:
  96.     print("You're less than 30 years old.")
  97. elif age < 40:
  98.     print("You're less than 40 years old.")
  99. else:
  100.     print("Your age is something else.")
  101.  
  102. # separate if-statement for month-number
  103. if month == 7:
  104.     print("We're closed on July.")
  105.     print("Back open in August, welcome!")
  106.  
  107. print("Thank you for using the application!")
  108.  
  109. # NEW FILE
  110.  
  111. price = 200
  112. age = 21
  113.  
  114. # if user is under 18 years old
  115. if age < 18:
  116.     # replace the price with the old price * 0.9 (10% discount)
  117.     price = price * 0.9
  118. else:
  119.     # for customers 18 years old or more,
  120.     # price is the old price + 4.95 postage
  121.     price = price + 4.95
  122.  
  123. # since we are modifying the price only in the if/else
  124. # print out the price whatever it is now!
  125. print(f"Total sum: {price} €")
  126.  
  127. # NEW FILE
  128.  
  129. age = input("How old are you?\n")
  130. age = int(age)
  131.  
  132. # less than 30
  133. if age < 30:
  134.     print("Less than 30 years old.")
  135.  
  136. # 30 or more
  137. if age >= 30:
  138.     print("30 years old or more.")
  139.  
  140. # is age exactly 30 => use two == signs
  141. # because one = means creating a variable
  142. if age == 30:
  143.     print("Exactly 30 years old.")
  144.  
  145. # is age something else than exactly 30
  146. if age != 30:
  147.     print("You're not exactly 30 years old.")
  148.  
  149. # NEW FILE
  150.  
  151. # ask one number from user
  152. # remember always to convert your input into numeric format
  153. # because otherwise you compare text to numbers
  154. number1 = input("Give a number:\n")
  155. number1 = int(number1)
  156.  
  157. number2 = 234
  158.  
  159. # if one of these numbers is still text format
  160. # (no int/float used), this can be very buggy
  161. if number1 > number2:
  162.     print("The first number is bigger!")
  163. else:
  164.     print("The second number is bigger")
  165.  
  166. # NEW FILE
  167.  
  168. # ask user a sales code
  169. sales_code = input("Give your discount code:\n")
  170.  
  171. current_code = "WINTER24"
  172.  
  173. # for example if "FALL23" == "WINTER24"
  174. # we can't use > or < with text, since
  175. # if "banana" > "firetruck" -> doesn't make any sense
  176. if sales_code == current_code:
  177.     print("Discount to price, -20%!")
  178. else:
  179.     print("Normal price.")
  180.  
  181. # NEW FILE
  182.  
  183. # ask user the product
  184. drink = input("What would you like to drink?\n")
  185.  
  186. # if-elif-else -> structure
  187. # -> react to different user responses
  188. # else -> drink not found
  189.  
  190. if drink == "milk":
  191.     print("Price of milk: 1 €")
  192. elif drink == "coffee":
  193.     print("Price of coffee: 7 €")
  194. elif drink == "water":
  195.     print("Free, drink from the tap.")
  196. else:
  197.     print("Drink not found.")
  198.  
  199. # NEW FILE
  200.  
  201. # ask the user if they are student or not
  202. choice = input("Are you a student? (s/o)\n")
  203.  
  204. # if is student
  205. if choice == "s":
  206.     print("This code only considers students")
  207.     print("For example, calculate ticket price etc.")
  208. elif choice == "o":
  209.     print("Calculate price for other customers etc.")
  210. else:
  211.     print("Incorrect selection. Please restart the application.")
  212.  
  213. # NEW FILE
  214.  
  215. number = input("Give a number:\n")
  216. number = int(number)
  217.  
  218. # if remainder of the number when dividing by 2
  219. # is exactly zero => even number
  220. # if not (else) => odd number
  221. if number % 2 == 0:
  222.     print("Even number")
  223. else:
  224.     print("Odd number")
  225.  
  226. # NEW FILE
  227.  
  228. number = input("Give a number:\n")
  229. number = int(number)
  230.  
  231. # is number between 0 and 30, or in the range 0-30
  232. if number > 0 and number < 30:
  233.     print("Number is in the range 0-30")
  234.  
  235. # is number less than 0 OR more than 30
  236. if number < 0 or number > 30:
  237.     print("Number is less than 0, or more than 30")
  238.     print("Basically, this is the opposite of the previous statement.")
  239.  
  240. # NEW FILE
  241.  
  242. number = input("Give a number:\n")
  243. number = int(number)
  244.  
  245. # is number between 0 and 30, or in the range 0-30
  246. # this is the recommended shorthand in Python
  247. # for numeric range checks
  248. if 0 <= number <= 30:
  249.     print("Number is in the range 0-30")
  250.  
  251. # NEW FILE
  252.  
  253. year = input("Give a year (2000-2012)\n")
  254. year = int(year)
  255.  
  256. # easy to compare a bunch of range values
  257. # always double-check all comparisons
  258. # and try your with test values
  259. # ESPECIALLY THE BOUNDARY VALUES
  260. # for example, 2004, 2008, 2012
  261. if 2000 <= year < 2004:
  262.     print("2000-2004")
  263. elif 2004 <= year < 2008:
  264.     print("2004-2008")
  265. elif 2008 <= year < 2012:
  266.     print("2008-2012")
  267. else:
  268.     print("Incorrect year.")
  269.  
  270. # NEW FILE
  271.  
  272. age = 20
  273. city = "Rovaniemi"
  274. student = True
  275.  
  276. # condition that is depending on three different variables
  277. if age >= 18 and city == "Rovaniemi" and student == True:
  278.     print("An adult student from Rovaniemi")
  279.     print("If for some reason you need to have a very specific condition")
  280.     print("For example, marketing or information purposes, you can do this.")
  281.  
  282. print("Thank you for using our application.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement