Advertisement
tuomasvaltanen

Untitled

Sep 30th, 2024 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.26 KB | None | 0 0
  1. # 30.9.2024, more conditional statements
  2. print("Welcome!")
  3.  
  4. # NEW FILE
  5.  
  6. # our example variables, we could ask these
  7. # also from the user
  8. age = 22
  9. city = "Helsinki"
  10.  
  11. # our code should give instructions to adult
  12. # users regarding their hometown and its health care services
  13. # underage users should be directed to use their own school's
  14. # health care services instead
  15. if age >= 18:
  16.     print("Adult instructions here!")
  17.  
  18.     # this if-statement is only run, if the user is an adult (over 18)
  19.     if city == "Rovaniemi":
  20.         print("Health care address for adults: Test Alley 12.")
  21.     elif city == "Helsinki":
  22.         print("Health care address for adults: Somewhere Road 52")
  23.  
  24. else:
  25.     print("Underage students: contact your school's health care services!")
  26.  
  27. # NEW FILE
  28.  
  29. # Application description: Calculate train ticket price. The logic is this:
  30. # Students get always 50% discount
  31. # Other users pay full price + 2.5 € service fee
  32. # if the ticket is over 100 €, no service fee
  33.  
  34. # ask user the needed variables, convert price to decimal format
  35. status = input("Student or other? (s/o)\n")
  36. price = input("Original ticket price? (€)\n")
  37. price = float(price)
  38.  
  39. # make two different lanes for students and other users
  40. if status == "s":
  41.     # students get -50% off the price
  42.     price = price * 0.5
  43.  
  44. elif status == "o":
  45.     # Other users pay full price + 2.5 € service fee
  46.     # if the ticket is over 100 €, no service fee
  47.  
  48.     # in other words: only add service fee if price is under 100
  49.     if price < 100:
  50.         price = price + 2.5
  51.  
  52. # round the value to two decimals (because it's money)
  53. price = round(price, 2)
  54. print(f"Final ticket price: {price} €")
  55.  
  56. # NEW FILE
  57.  
  58. # example variables
  59. # humidity, 0-100%
  60. humidity = 88
  61. temperature = -5
  62.  
  63. # initialize the control boolean variable: raining
  64. # the only purpose of this variable is to keep track of one question:
  65. # is it raining or not at the moment
  66. raining = False
  67.  
  68. # HERE YOU WOULD HAVE ALL THE POSSIBLE LOGIC
  69. # THAT CAN ALTER THE CONDITION OF THE raining-variable
  70. # FOR EXAMPLE 300-500 lines of code
  71. if humidity > 80:
  72.     raining = True
  73.  
  74. # another check, if temperature is subzero, it's no longer rain
  75. if temperature < 0:
  76.     raining = False
  77.  
  78. # with boolean logic, you usually have an if-statement
  79. # like this that handles the final situation of the boolean variable
  80. # if raining ===> if raining == True
  81. # if not raining ===> if raining == False
  82. if raining:
  83.     print("It rains!")
  84. else:
  85.     print("It doesn't rain.")
  86.  
  87.  
  88. # NEW FILE
  89.  
  90. # EXERCISE DESCRIPTION:
  91.  
  92. # Make an application that determines based on variables
  93. # if it's a GOOD or BAD weather outside
  94.  
  95. # The logic of the weather is this:
  96.  
  97. # Bad weather: if temperature is less than +10C
  98. # Bad weather: if humidity is over 80%
  99. # Bad weather: if wind speed is over 2.5m/s
  100. # Bad weather: if it's dark outside
  101. # in this case, we can assume it's dark outside, if time is
  102. # between 20-24 or 0-7
  103.  
  104. # initialize variables
  105. temperature = 13
  106. humidity = 65
  107. wind_speed = 1.5
  108. hour = 12
  109.  
  110. # helper variables
  111. sun_down = 20
  112. sun_rises = 7
  113.  
  114. # initialize our Boolean variable, that only keeps track of if the weather is good or not
  115. # assume in the beginning the weather is good, and try to prove it otherwise with
  116. # subsequent conditions
  117. good_weather = True
  118.  
  119. # let's try to formulate this condition somehow
  120. # if you start to have a condition like this, consider a helper Boolean variable
  121. # to make the logic easier and more manageable
  122. # if temperature < 10 or humidity > 80 or wind_speed > 2.5 or (time > sun_down and  ....)
  123.  
  124. # if temperature less than 10
  125. if temperature < 10:
  126.     good_weather = False
  127.  
  128. # if humidity over 80%
  129. if humidity > 80:
  130.     good_weather = False
  131.  
  132. # wind_speed check
  133. if wind_speed > 2.5:
  134.     good_weather = False
  135.  
  136. # most difficult condition: time regarding dark
  137. if hour > sun_down or hour < sun_rises:
  138.     good_weather = False
  139.  
  140.  
  141. # all checks done, we can now use the Boolean for the final time
  142. if good_weather:
  143.     print("Good weather outside!")
  144. else:
  145.     print("Bad weather... :(")
  146.  
  147. # NEW FILE
  148.  
  149. # Exercise 1. Create an application that greets the user based on given
  150. # hour of the current time. Convert the given hour into integer
  151. # format.  Use the following logic while greeting the user:
  152.  
  153. # If hour is 05 - 11 : respond "Good morning"
  154. # If hour is 12 - 17 : respond "Good afternoon"
  155. # If hour is 18 - 21 : respond "Good evening"
  156. # If hour is 22 - 04 : respond "Good night"
  157.  
  158. # VERSION 1: without the advanced feature
  159.  
  160. # PHASE 1: Ask the needed variables from user
  161. hour = input("Give the current hour:\n")
  162. hour = int(hour)
  163.  
  164. # PHASE 2: Program logic
  165. # greet the user based on instructions above
  166. if 5 <= hour <= 11:
  167.     print("Good morning!")
  168. elif 12 <= hour <= 17:
  169.     print("Good afternoon!")
  170. elif 18 <= hour <= 21:
  171.     print("Good evening!")
  172. else:
  173.     print("Good night!")
  174.  
  175. # NEW FILE
  176.  
  177. # 1. Create an application that greets the user based on given
  178. # hour of the current time. Convert the given hour into integer
  179. # format.  Use the following logic while greeting the user:
  180.  
  181. # If hour is 05 - 11 : respond "Good morning"
  182. # If hour is 12 - 17 : respond "Good afternoon"
  183. # If hour is 18 - 21 : respond "Good evening"
  184. # If hour is 22 - 04 : respond "Good night"
  185.  
  186. # Advanced feature:
  187. # If the user doesn't give a time (empty text), use automatically
  188. # the current hour of time (by using datetime-module). If the user
  189. # chose the automatic hour, inform user:
  190. # "Using current hour automatically."
  191.  
  192. # VERSION 2: automatic hour selection before the greeting supported
  193.  
  194. from datetime import datetime
  195.  
  196. # PHASE 1: Ask the needed variables from user
  197. hour = input("Give the current hour:\n")
  198.  
  199. # if the user gave an empty text => use automatic time
  200. # whether the code goes to if or else, the end result
  201. # will be an integer variable called hour
  202. if hour == "":
  203.     # empty input => get current time from datetime
  204.     timestamp = datetime.now()
  205.     hour = int(timestamp.hour)
  206.     print(f"Using current hour automatically: {hour}")
  207. else:
  208.     # use the user's original time
  209.     hour = int(hour)
  210.  
  211. # in this spot, the hour has to be an integer
  212. # and the following will work correctly
  213.  
  214. # PHASE 2: Program logic
  215. # greet the user based on instructions above
  216. if 5 <= hour <= 11:
  217.     print("Good morning!")
  218. elif 12 <= hour <= 17:
  219.     print("Good afternoon!")
  220. elif 18 <= hour <= 21:
  221.     print("Good evening!")
  222. else:
  223.     print("Good night!")
  224.  
  225. # NEW FILE
  226.  
  227. # 2. Create a simple calculator application, where user can perform the
  228. # following operations: +, -, * and /.
  229. #
  230. # In the beginning, ask the user two different numbers in float-format,
  231. # and ask finally which operation they would like to perform between the numbers.
  232. # After the questions, perform the selected operation between the numbers,
  233. # if the selections were correct.
  234.  
  235. # If user selects division (/), check that the second number of the user
  236. # is not 0. If user still gives a 0 as a second number, inform the user that
  237. # we can't divide by zero. If user selects an operation that is not supported,
  238. # inform the user: "Incorrect operation".
  239.  
  240. # Print the end result rounded to one decimal.
  241.  
  242. # PHASE 1: Get needed variables from user and convert data types
  243. number1 = input("Give first number:\n")
  244. number1 = float(number1)
  245.  
  246. number2 = input("Give second number:\n")
  247. number2 = float(number2)
  248.  
  249. operation = input("Which operation would you like to perform? (+, -, *, /)\n")
  250.  
  251. # initialize a variable for the final result
  252. result = 0
  253.  
  254. # PHASE 2: Program logic
  255. if operation == "+":
  256.     result = number1 + number2
  257. elif operation == "-":
  258.     result = number1 - number2
  259. elif operation == "*":
  260.     result = number1 * number2
  261. elif operation == "/":
  262.     if number2 == 0:
  263.         print("Can't divide by zero.")
  264.     else:
  265.         result = number1 / number2
  266. else:
  267.     print("Incorrect operation")
  268.  
  269. # PHASE 3: round values and print result
  270. result = round(result, 1)
  271. print(f"Result: {result}")
  272.  
  273. # NEW FILE
  274.  
  275. # 3. Create an application that asks the user the name of the
  276. # product and the amount of products they wish to buy. Also ask
  277. # the user whether they have a discount voucher (yes/no).
  278.  
  279. # The pricing is the following:
  280. # - smartphone = 599 € per item
  281. # - computer = 899 € per item
  282. # - coffee machine = 129 € per item
  283.  
  284. # Calculate the total price of the user's products.
  285.  
  286. # If the user has a discount voucher, reduce 15% from the total price.
  287. # Finally, print the price rounded to two decimals, and inform them if a
  288. # voucher was used to reduce the price.
  289.  
  290. # PHASE 1: Ask needed variables from user
  291. product = input("Which product you wish to buy? (smartphone, computer, coffee machine)\n")
  292. amount = input("How many items of this product would you like to buy?\n")
  293. amount = int(amount)
  294.  
  295. # voucher, yes/no
  296. discount = input("Do you have a discount voucher? (yes/no)\n")
  297.  
  298. # initialize result variable
  299. total = 0
  300.  
  301. # PHASE 2: price logic
  302. if product == "smartphone":
  303.     total = amount * 599
  304. elif product == "computer":
  305.     total = amount * 899
  306. elif product == "coffee machine":
  307.     total = amount * 129
  308. else:
  309.     print("Unidentified product.")
  310.  
  311. # voucher logic
  312. if discount == "yes":
  313.     # 15% discount => 1 - 0.15 => 0.85
  314.     total = total * 0.85
  315.  
  316. # PHASE 3: round values and print
  317. total = round(total, 2)
  318. print(f"Total price: {total} €")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement