-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditionals_Practice.py
More file actions
367 lines (302 loc) · 10.7 KB
/
Copy pathConditionals_Practice.py
File metadata and controls
367 lines (302 loc) · 10.7 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
mystery_int_1 = 7
mystery_int_2 = 2
# You may modify the lines of code above, but don't move them!
# When you Submit your code, we'll change these lines to
# assign different values to the variables.
# The variables below hold two integers, mystery_int_1 and
# mystery_int_2. Complete this program below such that it
# prints "Factors!" if either of the numbers is a factor of
# the other. If neither number is a factor of the other,
# do not print anything.
#
# Hint: You can do this with just one conditional statement
# by using the logical expressions we learned earlier (and,
# or, and not). You'll also use the modulus operator we
# learned in Chapter 2.4.
# Add your code here!
if mystery_int_1 % mystery_int_2 == 0:
print('Factors!')
elif mystery_int_2 % mystery_int_1 == 0:
print('Factors!')
else:
print('Not factors :(')
hunger = True
boredom = False
if hunger == True and boredom == True:
print("Let's order pizza!")
else:
print("Let's wait until dinnertime.")
mystery_state = "Georgia"
# You may modify the lines of code above, but don't move them!
# When you Submit your code, we'll change these lines to
# assign different values to the variables.
# It's snowing!
#
# The variable above holds the name of the state that you're
# in (hypothetically). Complete the code below so that it
# prints the following messages depending on what state you're
# in:
#
# - "School isn't cancelled." if we're in New Jersey
# - "School is postponed." if we're in North Carolina
# - "School is cancelled!" if we're in Georgia
# - "idk wear a sweater" if we're in any other state (or if
# the value doesn't represent a valid state).
# Add your code here!
if mystery_state == 'New Jersey':
print("School isn't cancelled.")
elif mystery_state == 'North Carolina':
print("School is postponed.")
elif mystery_state == 'Georgia':
print("School is cancelled.")
else:
print("idk wear a sweater")
print()
mystery_string = "zizazzle"
# You may modify the lines of code above, but don't move them!
# When you Submit your code, we'll change these lines to
# assign different values to the variables.
# The variable above creates a string. Add some code below
# that will print based on the maximum number of consecutive
# z's in the string:
#
# - If z appears three or more times in a row, print "I'm sleepy..."
# - If z appears two times in a row, print "I love ZZ Top!"
# - If z appears once, print "One is the loneliest number."
# - If z does not appear, print "Who needs z anyway?"
#
# The message you print should correspond to the most
# consecutive z's: in the original value of mystery_string,
# for example, you'd print "I love ZZ Top!" because there are
# two consecutive z's, even though there are also some
# individual z's.
#
# Ignore upper-case z's -- only look for lower-case z's.
#
# Hint: Remember the 'in' operator! It returns true if the
# first string is found within the second string. For example,
# "bog" in "boggle" would return True, but "bog" in "artemis"
# would return False.
if 'zzz' in mystery_string:
print("I'm sleepy...")
elif 'zz' in mystery_string:
print("I love ZZ Top!")
elif 'z' in mystery_string:
print("One is the loneliest number.")
else:
print("Who needs z anyway?")
print()
clean_shirts = ["blue", "striped", "black", "maroon"]
weather = "raining"
desired_shirt = "striped"
raincoat_clean = True
print("Can I wear my striped shirt and raincoat today?")
if desired_shirt in clean_shirts and weather == "raining" and raincoat_clean == True:
print("Yes!")
else:
print("Nope; try a different outfit.")
print()
team_3 = "Georgia Tech"
team_3_score = 30
team_4 = "Georgia"
team_4_score = 40
# You may modify the lines of code above, but don't move them!
# When you Submit your code, we'll change these lines to
# assign different values to the variables.
# Above we've created four variables: two team names and two
# scores. A team wins if their score is greater than the other
# team's score.
#
# Add to the code below. To print a messages like these
# depending on the values:
#
# - If one team beat the other, print:
# "[winner] beat [loser] by [margin]"
# - If neither team won, print:
# "[team_1] played [team_2] and it was a tie"
#
# For example, with the original values in this code, you
# should print "Georgia Tech beat Georgia by 1"
#
# Hint: to make this easier, create three variables: winner,
# loser, and margin. Then, find their values before worrying
# about printing the final message.
margin = team_3_score - team_4_score
margin = abs(margin)
if team_3_score == team_4_score:
print(team_3 + " played " + team_4 + " and it was a tie")
elif team_3_score > team_4_score:
print(team_3 + " beat " + team_4 + " by " + str(margin))
elif team_3_score < team_4_score:
print(team_4 + " beat " + team_3 + " by " + str(margin))
print()
# another way...
team_1 = "Georgia Tech"
team_1_score = 28
team_2 = "Georgia"
team_2_score = 27
# What we actually print is based on the which team's score
# is greater and what the margin of victory is. So, the first
# thing we might want to do is identify who is the winner and
# who is the loser:
if team_1_score > team_2_score:
winner = team_1
loser = team_2
margin = team_1_score - team_2_score
else:
winner = team_2
loser = team_1
margin = team_1_score - team_2_score
# Here, we're labeling a team as the 'winner' even if the game
# was tied: we'll take care of that in the next conditional.
# Now, we want to print one message if the score was tied, and
# a different one if it wasn't:
if margin == 0:
print(team_1, "played", team_2, "and it was a tie")
else:
print(winner, "beat", loser, "by", margin)
# Notice we're using Python's comma syntax for print statements
# here. That's why we don't have to convert margin to an
# integer. We could also rewrite that last print statement like
# this:
steak = False
pork = True
guacamole = False
queso = False
# You may modify the lines of code above, but don't move them!
# When you Submit your code, we'll change these lines to
# assign different values to the variables.
#
# Imagine you're writing code for a cash register at a
# restaurant. This restaurant serves burritoes. The base price
# of a burrito is $5. If the customer wants steak or pork, it
# adds $0.50. If they want quacamole, it adds $1.00. If they
# want queso, it adds $1.00. The customer may only select one
# meat, but they may have both queso and guacamole, neither,
# or just one.
#
# Write some code below that will print the cost of the
# burrito based on the variables above. You do not need to
# print the dollar sign or extra 0s. Remember, your final answer
# should only print out the price: comment out any debug
# statements once you have the right answer.
price = 5.00
if steak or pork:
price += 0.50
if guacamole:
price += 1.00
if queso:
price += 1.00
print(price)
print()
time = 2359
tiredness_level = 90
homework_done = True
early_class = False
time = 2359
tiredness_level = 90
homework_done = True
early_class = False
if time >= 2300:
if tiredness_level >= 85:
if homework_done == True:
if early_class == True:
print("Go to sleep!")
else:
print("Go to sleep soon...")
else:
print("Finish your work, then sleep!")
else:
print("Stay up a little longer!")
else:
print("It's still pretty early.")
sunny = True
windy = True
# You may modify the lines of code above, but don't move them!
# When you Submit your code, we'll change these lines to
# assign different values to the variables.
# Below, we've gone ahead and written some code that uses a
# conditional to print a message based on the variables above.
# Revise this code so that it uses nested conditionals instead
# of the 'and' operator. There should be _no_ instances of the
# 'and' reserved word in your code, but it should behave the
# same as it did originally.
if sunny:
if not windy:
print("Wear a hat!")
if sunny:
if windy:
print("Enjoy the breeze!")
print()
# You may modify the lines of code above, but don't move them!
# When you Submit your code, we'll change these lines to
# assign different values to the variables.
# Above we've created three variables representing an attempt
# to enter a password. Add some code below that will print the
# result of this check:
#
# - "Login successful." if entered is the same as password
# and tries is less than or equal to 3.
# - "Incorrect password." if entered is not the same as
# password, but tries is less than or equal to 3.
# - "Tries exceeded." if tries is greater than 3.
#
# You don't need to worry about incrementing tries if the
# password is incorrect.
entered = "abc123"
password = "abc123"
tries = 3
if entered == password:
if tries <= 3: # not needed
print("Login successful.")
elif entered != password:
if tries <= 3: # not needed
print("Incorrect password.")
if tries > 3:
print("Tries exceeded.")
print()
# Another method
if tries > 3:
print("Tries exceeded.")
# Now, if the user has run out of tries, the next two blocks
# will never trigger because they are 'else' blocks to the
# preceding 'if'. So, we don't need to recheck whether tries
# is greater than 3: if we've reached the next blocks, it
# isn't. So instead, we can just check whether the password
# is correct:
elif password == entered:
print("Login successful.")
else:
print("Incorrect password.")
# We could also design this a little differently byusing
# nested conditionals:
if tries > 3:
print("Tries exceeded.")
else:
if password == entered:
print("Login successful.")
else:
print("Incorrect password.")
print()
todaysWeather = "cold"
if todaysWeather == "cold" and todaysWeather == "windy":
print("jacket")
print("scarf")
print("done")
balance = 20
purchase = 19
salesTax = 1.08
if balance >= purchase * salesTax:
print("Purchase possible")
else:
print("Purchase not possible")
print("done")
print()
gallons_of_milk = 2
dozens_of_eggs = 10
if gallons_of_milk and dozens_of_eggs <=6:
print( "Time to take a trip to the store")
elif gallons_of_milk or dozens_of_eggs <=3:
print("We need to stock up!")
else:
print( "We have everything we need!")