Advertisement
tuomasvaltanen

Untitled

Sep 29th, 2023 (edited)
902
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.99 KB | None | 0 0
  1. # lecture 3, 29.9.2023, Conditional statements
  2. print("Welcome!")
  3.  
  4. # NEW FILE
  5.  
  6. age = 19
  7.  
  8. # conditional statement -> is age under 20?
  9. # if it is -> run the print-code
  10. # if not, skip all the code inside the if-statement
  11. if age < 20:
  12.     print("You are less than 20 years old.")
  13.     print("This is also printed, if age is less than 20")
  14.     print("There's no limit on how much code is inside a conditional statement.")
  15.  
  16. print("This is printed, no matter the case (not inside the if-statement)")
  17.  
  18. # NEW FILE
  19.  
  20. # ask user for the age
  21. age = input("How old are you?\n")
  22. age = int(age)
  23.  
  24. # if age is less than 20 -> print text
  25. # else (in other case) -> print something else
  26. # else is basically the opposite of if age < 20 at this point
  27. # => 20 years old or more
  28. if age < 20:
  29.     print("You're under 20 years old")
  30. else:
  31.     print("You're something else (basically 20 years old or more)")
  32.  
  33. # NEW FILE
  34.  
  35. # ask user for the age
  36. age = input("How old are you?\n")
  37. age = int(age)
  38.  
  39. # if age is less than 20 -> print text
  40. # else (in other case) -> print something else
  41. # else is basically the opposite of if age < 20 at this point
  42. # => 20 years old or more
  43. if age < 20:
  44.     print("You're under 20 years old")
  45. elif age < 30:
  46.     print("You're under 30 years old")
  47. elif age < 40:
  48.     print("You're under 40 years old")
  49. else:
  50.     print("Your age is something else.")
  51.  
  52. print("Thank you for using the application!")
  53.  
  54. # NEW FILE
  55.  
  56. # ask user for the age and month
  57. age = input("How old are you?\n")
  58. age = int(age)
  59.  
  60. month = input("Provide month (number):\n")
  61. month = int(month)
  62.  
  63. # if age is less than 20 -> print text
  64. # else (in other case) -> print something else
  65. # else is basically the opposite of if age < 20 at this point
  66. # => 20 years old or more
  67. if age < 20:
  68.     print("You're under 20 years old")
  69. elif age < 30:
  70.     print("You're under 30 years old")
  71. elif age < 40:
  72.     print("You're under 40 years old")
  73. else:
  74.     print("Your age is something else.")
  75.  
  76. # different if-statement, always checked separately
  77. # of the if-statement above
  78. if month == 7:
  79.     print("Sorry, we're on vacation.")
  80.     print("Shop open again in August.")
  81.  
  82.  
  83. print("Thank you for using the application!")
  84.  
  85. # NEW FILE
  86.  
  87. # ask user the price in float format
  88. price = input("Give the price:\n")
  89. price = float(price)
  90.  
  91. age = input("How old are you?\n")
  92. age = int(age)
  93.  
  94. # modify price based on age
  95. if age < 18:
  96.     # if user under 18 -> discount to price 10%
  97.     price = price * 0.9
  98. else:
  99.     # if not under 18 -> no discount, add postage
  100.     price = price + 4.95
  101.  
  102. print(f"Total sum: {price} €")
  103.  
  104. # NEW FILE
  105.  
  106. # ask user for the age
  107. age = input("How old are you?\n")
  108. age = int(age)
  109.  
  110. if age < 30:
  111.     print("Less than 30 years old.")
  112.  
  113. # if possible, use this format most of the time
  114. # less than equal to
  115. # no need to do something age < 30 or age == 30: etc.
  116. if age <= 30:
  117.     print("Less than or equal to 30 years old.")
  118.  
  119. if age > 30:
  120.     print("More than 30 years old.")
  121.  
  122. # this is rarely need, usually we want to compare
  123. # to ranges instead of actual number values
  124. # sometimes needed, for example if product_id == 0
  125. # => a missing product in a webshop
  126. if age == 30:
  127.     print("You're exactly 30 years old")
  128.  
  129. # this is the opposite of == .... not EXACTLY 30 years old
  130. if age != 30:
  131.     print("You're not exactly 30 years old")
  132.  
  133. # NEW FILE
  134.  
  135. # ask one number from user
  136. # remember to always convert your input into int/float
  137. # comparing to text value creates strange bugs
  138. number1 = input("Give a number:\n")
  139. number1 = int(number1)
  140.  
  141. number2 = 234
  142.  
  143. # if one of these numbers is still in text-format
  144. # (no int/float used), this can be very buggy
  145. if number1 > number2:
  146.     print("The first number is bigger!")
  147. else:
  148.     print("The second number is bigger!")
  149.  
  150. # NEW FILE
  151.  
  152. # ask user for a sales code
  153. sales_code = input("Give your potential sales code:\n")
  154.  
  155. # this is our current sales code accepted for a discount
  156. # this season (a web shop for example)
  157. current_code = "WINTER23"
  158.  
  159. # let's see if the user's sales code is valid or not
  160. # usually text is compared only with == or !=
  161. # banana < firetruck wouldn't make any sense...
  162. if sales_code == current_code:
  163.     print("Discount to price, - 20%!")
  164. else:
  165.     print("Normal price.")
  166.  
  167. # NEW FILE
  168.  
  169. # ask for user input
  170. drink = input("What would like to drink?\n")
  171.  
  172. # if-elif -> react to different user responses
  173. # else -> drink not founc
  174. if drink == "milk":
  175.     print("Price of milk: 1€")
  176. elif drink == "coffee":
  177.     print("One pack of coffee: 6.5€")
  178. elif drink == "water":
  179.     print("Free, use the tap water.")
  180. else:
  181.     print("Product not found.")
  182.  
  183. # NEW FILE
  184.  
  185. # let's make a simple two-path application
  186. # ask user if they are student or not
  187.  
  188. choice = input("Are you a student or not? (s/o)\n")
  189.  
  190. # if student
  191. if choice == 's':
  192.     print("This code only considers students")
  193.     print("e.g. calculate some kind of special price etc.")
  194. elif choice == 'o':
  195.     # if other user
  196.     print("Calculate price for other customers")
  197. else:
  198.     print("Incorrect selection. Please restart the program.")
  199.  
  200. # NEW FILE
  201.  
  202. # ask user for a number
  203. number = input("Give a number:\n")
  204. number = int(number)
  205.  
  206. # when we divide by two, if remainder is exactly == 0
  207. if number % 2 == 0:
  208.     print("Even number!")
  209. else:
  210.     print("Odd number!")
  211.  
  212. # NEW FILE
  213.  
  214. number = input("Give a number:\n")
  215. number = int(number)
  216.  
  217. # is number either 0, or 0-30
  218. if number >= 0 and number < 30:
  219.     print("Number is between 0 - 30")
  220.  
  221. # is number less than 0, or more than 30
  222. if number < 0 or number >= 30:
  223.     print("Number is less than 0, or 30 or over.")
  224.  
  225. # NEW FILE
  226.  
  227. number = input("Give a number:\n")
  228. number = int(number)
  229.  
  230. # a preferred shorthand for 0 - 30
  231. # compare to mathematics
  232. if 0 <= number < 30:
  233.     print("Number is between 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 30 or over.")
  238.  
  239. # NEW FILE
  240. year = int(input("Give a year (2000-2012):\n"))
  241.  
  242. # easy to compare a bunch of ranged values
  243. # always double-check all the comparisons
  244. # and try your code with test values
  245. # ESPECIALLY on the boundary values
  246. # example: 2000, 2004, 2008, 2012
  247. if 2000 <= year < 2004:
  248.     print("2000-2004")
  249. elif 2004 <= year < 2008:
  250.     print("2004-2008")
  251. elif 2008 <= year <= 2012:
  252.     print("2008-2012")
  253. else:
  254.     print("Incorrect year")
  255.  
  256. # NEW FILE
  257.  
  258. # some variables
  259. # you could ask these from user as well
  260.  
  261. age = 20
  262. city = "Helsinki"
  263. student = True
  264.  
  265. # also possible to combine many variables if needed
  266. # typically "less is more" is the way to go
  267. if age >= 18 and city == "Rovaniemi" and student == True:
  268.     print("An adult student from Rovaniemi")
  269. else:
  270.     print("Something else.")
  271.  
  272. # NEW FILE
  273.  
  274. age = 20
  275. city = "Helsinki"
  276.  
  277. # first filter out users that are 18 or more
  278. if age >= 18:
  279.     # this part of code only considers adult users
  280.     print("Adult!")
  281.  
  282.     if city == "Rovaniemi":
  283.         print("Adult health care in address Test Street 12.")
  284.  
  285.     if city == "Helsinki":
  286.         print("Adult health care in address Experiment Plaza 35.")
  287.         print("Easiest to arrive by tram or bus.")
  288.  
  289. else:
  290.     # under 18 users get this response instead
  291.     # all of the if-statements above, will be ignored
  292.     print("For persons under 18 years old, consult your school health care.")
  293.  
  294. # NEW FILE
  295.  
  296. # ask user for price
  297. price = input("Price in EUR?\n")
  298. price = float(price)
  299.  
  300. # give starting value to discount to prevent variable scope errors
  301. discount = 0
  302.  
  303. # if price is over 300€ -> discount of 50€
  304. if price > 300:
  305.     discount = 50
  306. else:
  307.     # if price 300€ or less -> +10€ postage added
  308.     price = price + 10
  309.  
  310. # calculate final price including everything
  311. total = price - discount
  312.  
  313. print(f"Total sum: {total}€")
  314.  
  315. # NEW FILE
  316.  
  317. # variables, could be asked from user
  318.  
  319. # humidity 0-100%
  320. humidity = 92
  321. temperature = -5
  322. raining = False
  323.  
  324. if humidity > 80:
  325.     raining = True
  326.  
  327. # subzero temperatures, not actually rain anymore
  328. if temperature < 0:
  329.     raining = False
  330.  
  331. # THIS PART COULD POTENTIALLY HAVE HUNDREDS OF LINES OF CODE
  332. # SWITCHING THE RAINING BOOLEAN IN DIFFERENT POSITIONS
  333.  
  334. # final decision
  335. if raining:
  336.     print("It's raining outside!")
  337. else:
  338.     print("It's not raining at the moment.")
  339.  
  340. # NEW FILE
  341.  
  342. # ask the dtails from user
  343. status = input("Student or adult? (s/a)\n")
  344. price = input("Original ticket price?:\n")
  345. price = float(price)
  346.  
  347. # s == student
  348. # a == adult
  349. if status == 's':
  350.     # student get 50% discount
  351.     price = price * 0.5
  352. elif status == 'a':
  353.     # only add service fee
  354.     # if price is less than 100€
  355.     if price < 100:
  356.         price = price + 2.5
  357.  
  358. print(f"Final price: {price} €")
  359.  
  360. # NEW FILE
  361.  
  362. # EXERCISE DESCRIPTION:
  363.  
  364. # Let's make an application, that determines based on variables
  365. # if it's a GOOD or BAD weather outside
  366.  
  367. # The logic of the weather is this:
  368.  
  369. # Bad weather: if temperature is less than +10C
  370. # Bad weather: if humidity is over 80%
  371. # Bad weather: if wind speed is over 2.5 m/s
  372. # Bad weather: if it's dark outside
  373. # in this case we assume it's dark outside, if time is 20-24 or 0-7
  374.  
  375. # intialize variables
  376. temperature = 15
  377. humidity = 32
  378. wind_speed = 1.4
  379. time = 18
  380.  
  381. # the boolean that keeps track of bad/good weather
  382. good_weather = True
  383.  
  384. # helper variables
  385. sun_down = 20
  386. sun_rises = 7
  387.  
  388. # this condition gets very complex and unmaintainable
  389. # if you start to have a condition like this, consider a helper Boolean variable
  390. # if temperature < 10 or humidity > 80 or wind_speed > 2.5 or (time > sun_down ....)
  391.  
  392. # if temperature less than 10
  393. if temperature < 10:
  394.     good_weather = False
  395.  
  396. # humidity over 80%
  397. if humidity > 80:
  398.     good_weather = False
  399.  
  400. # is it too windy
  401. if wind_speed > 2.5:
  402.     good_weather = False
  403.  
  404. # is it dark at the moment
  405. if time > sun_down or time < sun_rises:
  406.     good_weather = False
  407.  
  408. # the final result - let's see what is the
  409. # result of the boolean in the end
  410. if good_weather:
  411.     print("Good weather outside!")
  412. else:
  413.     print("Bad weather .... :(")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement