Advertisement
tuomasvaltanen

Untitled

Sep 28th, 2022 (edited)
1,050
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.10 KB | None | 0 0
  1. # conditional statements today!
  2. print("Welcome!")
  3.  
  4. # NEW FILE
  5.  
  6. age = input("Give your age:\n")
  7. age = int(age)
  8.  
  9. # if/else - if age is larger than 20 or if it's something else
  10. if age > 20:
  11.     print("You're over 20 years old!")
  12.     print("We're inside the if-statement now!")
  13. else:
  14.     print("You're 20 years or less years old.")
  15.  
  16. print("Thank you for using the application!")
  17.  
  18. # NEW FILE
  19.  
  20. age = input("Give your age:\n")
  21. age = int(age)
  22.  
  23. # if/else - if age is larger than 20 or if it's something else
  24. if age < 20:
  25.     print("You're less than 20 years old!")
  26. elif age < 30:
  27.     print("You're less than 30 years old!")
  28. else:
  29.     print("You're over 30 years old.")
  30.  
  31. print("Thank you for using the application!")
  32.  
  33. # NEW FILE
  34.  
  35. age = input("What's your age again?\n")
  36. age = int(age)
  37.  
  38. # is age less or equal to 30
  39. if age <= 30:
  40.     print("30 or less years old")
  41.  
  42. # is age greater than 30
  43. if age > 30:
  44.     print("More than 30 years old")
  45.  
  46. # is age EXACTLY 30
  47. if age == 30:
  48.     print("You're exactly 30 years old")
  49.  
  50. # is age something else than EXACTLY 30
  51. if age != 30:
  52.     print("You're not exactly 30 years old.")
  53.  
  54. # NEW FILE
  55.  
  56. # two number variables for later comparison
  57. number1 = input("Give a number:\n")
  58. number1 = int(number1)
  59.  
  60. number2 = 123
  61.  
  62. # which variable has a bigger value?
  63. if number1 > number2:
  64.     print("First number is bigger!")
  65. else:
  66.     print("Second number is bigger!")
  67.  
  68. # NEW FILE
  69.  
  70. # does the user know the secret word
  71. secret_word = "banana"
  72.  
  73. text = input("Give us a word:\n")
  74.  
  75. # just like any variable, comparison
  76. if text == secret_word:
  77.     print("SECRET WORD IS BANANA!")
  78. else:
  79.     print("You don't know the secret word...")
  80.  
  81. # NEW FILE
  82.  
  83. text = input("Which drink you want?\n")
  84.  
  85. # react to different user answers
  86. if text == "milk":
  87.     print("Milk is 1.5 € per liter")
  88. elif text == "coffee":
  89.     print("Coffee is 7 € per 500g")
  90. elif text == "water":
  91.     print("Just use the tap water, will you?")
  92. else:
  93.     print("We don't have that drink, SORRY!")
  94.  
  95. # NEW FILE
  96.  
  97. choice = input("Are you a student? (y/n)\n")
  98.  
  99. # did user choose y or n
  100. # we could use else, but then "banana" would also result to "n"
  101. if choice == "y":
  102.     print("You're a student!")
  103.     print("You get a 50% discount from train ticket!")
  104. elif choice == "n":
  105.     print("Not a student, full price + postage!")
  106.  
  107. print("Thank you!")
  108.  
  109. # NEW FILE
  110.  
  111. number = input("Give a number:\n")
  112. number = int(number)
  113.  
  114. # if the remainder when dividing by 2 is 0 ==> Even number
  115. # otherwise, odd number
  116. if number % 2 == 0:
  117.     print("EVEN NUMBER")
  118. else:
  119.     print("ODD NUMBER")
  120.  
  121. # NEW FILE
  122.  
  123. number = input("Give a number:\n")
  124. number = int(number)
  125.  
  126. # preferred way for this is:
  127. # if 0 < number < 30:
  128. if number > 0 and number < 30:
  129.     print("Number is between 0 and 30")
  130.  
  131. if number < 0 or number > 30:
  132.     print("Number is outside 0 and 30")
  133.  
  134. # NEW FILE
  135.  
  136. age = 20
  137. city = "London"
  138. student = True
  139.  
  140. # this if-statement is only run if ALL OF THE variables are correct
  141. # in the condition!
  142. if age >= 18 and city == "Rovaniemi" and student == True:
  143.     print("Adult, student, from Rovaniemi.")
  144.  
  145. print("Thank you!")
  146.  
  147. # NEW FILE
  148.  
  149. # ask integer from user
  150. age = input("Age:\n")
  151. age = int(age)
  152.  
  153. # text is easy, just use input
  154. city = input("City:\n")
  155.  
  156. # with booleans we have to control the
  157. # variable ourselves with an if-statement
  158. student = True
  159.  
  160. choice = input("Student? (y/n):\n")
  161.  
  162. # change boolean to False if not a student
  163. # else is not needed, because the default value
  164. # of student is already True
  165. if choice == 'n':
  166.     student = False
  167.  
  168.  
  169. # this if-statement is only run if ALL OF THE variables are correct
  170. # in the condition!
  171. if age >= 18 and city == "Rovaniemi" and student == True:
  172.     print("Adult, student, from Rovaniemi.")
  173.  
  174. print("Thank you!")
  175.  
  176. # NEW FILE
  177.  
  178. # ask integer from user
  179. age = input("Age:\n")
  180. age = int(age)
  181.  
  182. # text is easy, just use input
  183. city = input("City:\n")
  184.  
  185. # with booleans we have to control the
  186. # variable ourselves with an if-statement
  187. student = True
  188.  
  189. choice = input("Student? (y/n):\n")
  190.  
  191. # change boolean to False if not a student
  192. # else is not needed, because the default value
  193. # of student is already True
  194. if choice == 'n':
  195.     student = False
  196.  
  197. # nested if statements. the city is only checked if age is more than 18
  198. if age >= 18:
  199.     print("Adult!")
  200.  
  201.     if city == "Rovaniemi":
  202.         print("Health care address: Test Alley 12")
  203.  
  204.     if city == "Helsinki":
  205.         print("Health care address: Business Street 5 A")
  206.  
  207. else:
  208.     print("Health care for underage people, ask your school.")
  209.  
  210. print("Thank you!")
  211.  
  212. # NEW FILE
  213.  
  214.  
  215. status = "student"
  216. price = 26
  217.  
  218. # different price logic for students and other customers
  219. # students get 50% discount, others have full price + 2.5€ service fee
  220. if status == "student":
  221.     print("Student price is 50%, minimum price is 15€")
  222.     price = price * 0.5
  223.  
  224.     # fix the price if it goes under 15 €
  225.     if price < 15:
  226.         price = 15
  227.  
  228. elif status == "other":
  229.     print("Other customers, full price + 2.5€ service fee")
  230.     price = price + 2.5
  231.  
  232. # print the price
  233. print(f"Price: {price} €")
  234.  
  235. # NEW FILE
  236.  
  237. temperature = input("Temperature?\n")
  238. temperature = int(temperature)
  239.  
  240. # initalize this variable with some default value
  241. # so it doesn't crash in the else-statement
  242. is_hot = False
  243.  
  244. if temperature > 20:
  245.     is_hot = True
  246.     print(temperature)
  247. else:
  248.     print(temperature)
  249.  
  250. print(is_hot)
  251.  
  252. # NEW FILE
  253.  
  254. # situation outside
  255. humidity = 90
  256. temperature = -5
  257. raining = False
  258.  
  259. # if humidity is large, it's raining
  260. if humidity > 80:
  261.     raining = True
  262.  
  263. # ... but if temperature is below zero
  264. # it's not rain anymore, it's hailing
  265. if temperature < 0:
  266.     raining = False
  267.  
  268. # HERE COULD BE HUNDREDS OF LINES OF OTHER CODE
  269.  
  270. # final check, is it raining or not?
  271. if raining == True:
  272.     print("It's raining!")
  273. else:
  274.     print("Not raining.")
  275.  
  276. # NEW FILE
  277. # MORE COMPLEX BOOLEAN EXAMPLE
  278.  
  279. # OUR APPLICATION'S LOGIC:
  280. # we have to determine, if weather outside is bad or not
  281.  
  282. # bad weather has temperature less than 10
  283. # bad weather has humidity more than 80
  284. # bad weather has wind speed more than 2.5
  285. # bad weather: it is dark outside
  286. # it's dark outside, if the time is between 20-24 or 0-7
  287.  
  288. # initalize variables, you can replace these with input too
  289. temperature = 8
  290. humidity = 88
  291. wind_speed = 5
  292. sun_goes_down = 20
  293. sun_rises = 7
  294. current_hour = 15
  295.  
  296. # assume it's a good weather in the beginning
  297. good_weather = True
  298.  
  299. # without booleans, this kinds of ridiculous conditional stuff happens
  300. # if temperature < 10 and humidity > 80 and wind_speed > 2.5 and (current_hour > sun_goes_down or ...)
  301.  
  302. # temperature check
  303. if temperature < 10:
  304.     good_weather = False
  305.  
  306. # humidity check
  307. if humidity > 80:
  308.     good_weather = False
  309.  
  310. # wind speed cheeck
  311. if wind_speed > 2.5:
  312.     good_weather = False
  313.  
  314. # time of day check
  315. if current_hour >= sun_goes_down or current_hour <= sun_rises:
  316.     good_weather = False
  317.  
  318. # final decision, is it a good weather outside?
  319. if good_weather:
  320.     print("It's a good weather outside!")
  321. else:
  322.     print("It's a bad weather... :(")
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement