-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditionals.py
More file actions
342 lines (254 loc) · 8.96 KB
/
Copy pathconditionals.py
File metadata and controls
342 lines (254 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#if statements let you respond to specific scenarios in code
#if statements also need to be followed by a colon
cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())
#at the core of every if statement is an expression that can be evualated as either true or false, and it is called a conditional test
#if a conditional test evualates as true, then Python will execute the code following the if statement, and if it is false, then it will ignore the code
#following the if statement
#capitalization matters. Two values with different capitalization aren't considered equal, even if they are the same word
#however, you can make a something equal to another value, for example, you can use the lower() function
#one equal sign sets a value to what comes after the equal sign, while two equal signs ask whether or not the two values before/after the equal sign are equal
car = ['Audi', 'bmw']
for car in car:
if car.lower() == 'audi':
print("true")
else:
print("false")
#if you want to check whether or not two values are equal, you can use a '!' and a '=' in tandem, like this '!='
#the exclamation point stands for 'not', so '!=' means 'does not equal'
requested_topping = 'mushrooms'
if requested_topping != 'anchovies':
print("\nHold the anchovies!")
age = 14
if age == '14':
print("\ntrue")
else:
print("false")
answer = 17
if answer != 42:
print("\nThat is not the right answer. Try again!")
age = 19
if age < 21:
print("\n21 is larger than age.")
if age <= 21:
print("\n21 is larger than or equal to age.")
if age > 21:
print("\n21 is smaller than age.")
if age >= 21:
print("\n21 is smaller than or equal to age.")
#to check if two conditions are true simultaneously, use the 'and' keyword to combine two conditional tests
age_0 = 22
age_1 = 18
if age_0 >= 21 and age_1 >= 21:
print("\ntrue")
else:
print("\nfalse")
age_1 = 22
if age_0 >= 21 and age_1 >= 21:
print("true")
else:
print("false")
#the keyword 'or' also allows you to check for different conditions, but evualates as true if either conditonal test passes; it doesn't need them both
#an 'or' expression only fails when both tests fail
age_0 = 22
age_1 = 18
if age_0 >= 21 or age_1 >= 21:
print("\ntrue")
else:
print("\nfalse")
age_0 = 18
if age_0 >= 21 or age_1 >= 21:
print("true")
else:
print("false")
#to see whether or not a specific value is already in a list, you can use the keyword 'in'
#you can use the key word 'not' in conjunction with the keyword 'in' to see if something is not in a list (the keywords are 'not in')
requested_toppings = ['mushrooms', 'onions', 'pineapple']
if 'mushrooms' in requested_toppings:
print("\ntrue")
if 'pepporoni' not in requested_toppings:
print("false")
banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users:
print(f"{user.title()}, you can post a response if you wish. Why can you post a response? Simple. Because you're not banned.")
#a boolean expression is another term for a conditional test
#CONDITIONAL TESTS PRACTICE
blessing = "Joni's Blessing"
print("\nIs blessing == Joni's Blessing? I think it is...")
print(blessing == "Joni's Blessing")
print("\nIs blessing == Salubra's Blessing? I think it isn't...")
print(blessing == "Salubra's Blessing")
age = 19
if age >= 18:
print("\nYou are old enough to vote! (In the US only, that is.... I don't know about anywhere else.)")
print("Have you registered to vote yet? (I say, knowing full well it's nowhere close to voting time.)")
#there are also if-else statements, which allow you to do an action if a conditional test fails.
#"else" must also be followed by a colon, and if you have another if after the else: statement, you have to indent it, and if you have another else after
#that, you have to indent that as well.
age = 17
if age >= 18:
print("\nYou are old enough to vote! (In the US only, that is.... I don't know about anywhere else.)")
print("Have you registered to vote yet? I say, knowing full well it's nowhere close to voting time.")
else:
print("\nSorry, you are too young to vote.")
print("You can vote when you turn 18 though, and I'm sure it won't be that long!")
#there are also if-elif-else syntaxes, which work by only executing a single action (Python will run each conditional test until one passes, and then it
#will execute the action for that test and ignore the rest of them.)
age = 12
if age < 4:
print("Your cost for admission is free.")
elif age < 18:
print("\nYour admission cost is $25.")
else:
print("Your admission cost is $40.")
age = 12
if age < 4:
price = 0
elif age < 18:
price = 25
else:
price = 40
print(f"\nYour admission cost is ${price}.")
#you can also add multiple elif blocks.
age = 12
if age < 4:
price = 0
elif age < 18:
price = 25
elif age < 65:
price = 40
else:
price = 20
print(f"\nYour admission cost is ${price}.")
#you also don't need to finish the if-elif chain with an else block.
if age < 4:
price = 0
elif age < 18:
price = 25
elif age < 65:
price = 40
elif age >= 65:
price = 20
print(f"\nYour admission cost is ${price}.")
#while the if-elif-else chain is useful, it is only useful when you only require only one condition to pass.
requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:
print("\nAdding mushrooms.")
if 'pepperoni' in requested_toppings:
print("Adding pepperoni.")
if 'extra cheese' in requested_toppings:
print("Adding extra cheese.")
print("\nFinished making your pizza!")
#If you want only one block of code to run, use an if-elif-else syntax; otherwise, use a series of independent if statements.
#ALIEN COLORS #1 PRACTICE
alien_color = 'red'
if alien_color == 'green':
print("You earned 5 points!")
if alien_color == 'red':
print("\nYou earned something!")
#ALIEN COLORS #2 PRACTICE
alien_color = 'neon black'
if alien_color == 'green':
print("You earned 5 points!")
else:
print("\nYou earned 10 points!")
print("On a separate note, I'm pretty sure the color 'neon black' doesn't exist.")
#ALIEN COLORS #3 PRACTICE
alien_color = 'red'
if alien_color == 'green':
score = 5
elif alien_color == 'yellow':
score = 10
elif alien_color == 'red':
score = 15
print(f"\nYou earned {score} points!")
age = 14
if age < 2:
life_stage = 'baby'
elif age >= 2 and age < 4:
life_stage = 'toddler'
elif age >= 4 and age < 13:
life_stage = 'kid'
elif age >= 13 and age < 20:
life_stage = 'teenager'
elif age >= 20 and age < 65:
life_stage = 'adult'
elif age >= 65:
life_stage = 'elder'
print(f"\nYou are a {life_stage}.")
favorite_foods = ['steak', 'ribs', 'lamb']
if 'steak' in favorite_foods:
print("\nYou really like steak!")
if 'ribs' in favorite_foods:
print("You really like ribs!")
if 'lamb' in favorite_foods:
print("You really like lamb!\n")
if 'chicken' in favorite_foods:
print("You really like chicken!")
if 'duck' in favorite_foods:
print("You really like duck!")
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for requested_topping in requested_toppings:
if requested_topping == 'green peppers':
print("Sorry, we're out of green peppers right now.")
else:
print(f"Adding {requested_topping}.")
print('\nFinished making your pizza!')
#You can also check if a list is empty
requested_toppings = []
if requested_toppings:
for requested_topping in requested_toppings:
print(f"Adding {requested_topping}.")
print('\nFinished making your pizza!')
else:
print("\nAre you sure you want a plain pizza?\n")
available_toppings = ['mushrooms', 'olives', 'green peppers', 'pepperoni', 'pineapple', 'extra cheese']
requested_toppings = ['mushrooms', 'french fries', 'extra cheese']
for requested_topping in requested_toppings:
if requested_topping in available_toppings:
print(f"Adding {requested_topping}.")
else:
print(f"Sorry, we don't have {requested_topping}.")
print("Finished making your pizza!")
#HELLO ADMIN PRACTICE
usernames = ['admin', 'cheesy', 'hollow_geek', 'fireb0rn', 'grog']
for username in usernames:
if username == 'admin':
print("\nHello admin. Would you like to see a status report?")
else:
print(f"Hello {username}, thank you for logging in again.")
#NO USERS PRACTICE
usernames = []
if usernames:
for username in usernames:
if username == 'admin':
print("\nHello admin. Would you like to see a status report?")
else:
print(f"Hello {username}, thank you for logging in again.")
else:
print("\nWe need to find some users.\n")
#CHECKING USERNAMES PRACTICE
current_users = ['bob', 'joe', 'gary', 'Tike', 'GG']
current_users_decap = [user.lower() for user in current_users]
new_users = ['bob', 'little', 'stuart', 'GG', 'gobble']
for new_user in new_users:
if new_user.lower() in current_users_decap:
print("This username has already been chosen.")
else:
print("This username is available.")
#ORDINAL NUMBERS PRACTICE
ordinal_numbers = list(range(1,10))
for ordinal_number in ordinal_numbers:
if ordinal_number == 1:
print("\n1st")
if ordinal_number == 2:
print("2nd")
elif ordinal_number == 3:
print("3rd")
else:
print(f"{ordinal_number}th")