 # Untitled

Sep 27th, 2021 (edited)
944
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # Adobe Connect -lecture, 27.9.2021
2. print("Conditional statements today!")
3.
4. # ask the user's age and convert it into an integer
5. age = input("How old are you?\n")
6. age = int(age)
7.
8. # check if age is less or at least 20
9. if age < 20:
10.     print("You're less than 20 years old.")
11. elif age < 30:
12.     print("You're less than 30 years old.")
13. else:
14.     print("You're at least 30 years old.")
15.
16. print("This is always printed, no matter what the age is!")
17.
18. # NEW FILE
19.
20. # ask the user's age and convert it into an integer
21. age = input("How old are you?\n")
22. age = int(age)
23.
24. # less than
25. if age < 30:
26.     print("You're less than 30 years old.")
27.
28. # more or exactly 30
29. if age >= 30:
30.     print("You're at least 30 years old.")
31.
32. # exactly 30
33. if age == 30:
34.     print("You are exactly 30 years old.")
35.
36. # anything else but 30
37. if age != 30:
38.     print("You're not exactly 30 years old.")
39.
40. # NEW FILE
41.
42. # comparing variables
43.
44. # ask number from user
45. number1 = input("Give a number:\n")
46. number1 = int(number1)
47. number2 = 234
48.
49. # compare it to the other variable
50. if number1 < number2:
51.     print("Number 1 is smaller.")
52. else:
53.     print("Number 2 is smaller.")
54.
55. # NEW FILE
56.
57. # checking for a word in if-statement
58. text = input("Give some text:\n")
59.
60. if text == "banana":
61.     print("You found the magic word!")
62. else:
63.     print("You didn't find it... :(")
64.
65.
66. # NEW FILE
67.
68. # a typical selection logic in many exercises
69. choice = input("Are you a student, or an adult? (s/a)\n")
70.
71. if choice == 's':
72.     print("Calculate the price for a student.")
73. elif choice == 'a':
74.     print("Calculate the price for an adult.")
75. else:
76.     print("Incorrect selection.")
77.
78. # NEW FILE
79.
80. # you can ask this from user too!
81. number = 12
82.
83. # a classic, is the number even or odd?
84. if number % 2 == 0:
85.     print("Even.")
86. else:
87.     print("Odd.")
88.
89. # NEW FILE
90.
91. number = input("Give number:\n")
92. number = int(number)
93.
94. # we can have multiple comparisons in a single statement
95. if 0 <= number <= 30:
96.     print("The number is between 0 and 30.")
97.
98. if number < 0 or number > 30:
99.     print("The number is outside 0 and 30.")
100.
101. # NEW FILE
102.
103. city = "Rovaniemi"
104. age = 20
105. student = True
106.
107. # comparing to multiple variables
108. # ALL OF THESE HAVE TO MATCH
109. if age >= 18 and city == 'Rovaniemi' and student == True:
110.     print("An adult student from Rovaniemi.")
111. else:
112.     print("Not an adult student from Rovaniemi.")
113.
114.
115. # NEW FILE
116. # nested if-statement,
117. # an if-statement inside another if-statement
118. number = 17
119.
120. if number % 2 == 0:
121.     print("Even")
122.
123.     if number > 10:
124.         print("That's a big even number!")
125. else:
126.     print("Odd")
127.
128. # NEW FILE
129.
130. # the variable scope example from slides
131. temperature = 17
132. is_hot = False
133.
134. if temperature > 20:
135.     is_hot = True
136.     print(temperature)
137. else:
138.     print(temperature)
139.
140. print(is_hot)
141.
142. # NEW FILE
144.
145. temperature = -12
146. humidity = 86
147.
148. # we assume at first, it's not raining
149. raining = False
150.
151. # very humid => raining
152. if humidity > 80:
153.     raining = True
154.
155. # if it's cold, it's not raining, it's a hailstorm
156. if temperature < 0:
157.     raining = False
158.
159. # THIS PART COULD HAVE HUNDREDS LINES OF CODE
160.
161. if raining:
162.     print("It rains!")
163. else:
164.     print("Doesn't rain.")
165.
166. # NEW FILE
167. # creating a boolean from input, example 1
168. # THIS CAN BE DONE IN MANY WAYS
169. choice = input("Is it raining? (y/n)\n")
170. raining = False
171.
172. if choice == 'y':
173.     raining = True
174.
175. if raining:
176.     print("RAINS!")
177. else:
178.     print("No rain.")
RAW Paste Data