CS: Python: Conditionals (if/elif/else)
Make decisions in code using if, elif, and else
CS: Python: Conditionals (if/elif/else)
Make decisions in code using if, elif, and else
CS - Grade 6-8
- 1
Trace this code: age = 12 if age >= 13: message = "teen" else: message = "not teen" What is the value of message? Explain why.
Check whether the condition age >= 13 is True or False.
The value of message is "not teen" because 12 is not greater than or equal to 13, so the else block runs. - 2
Write an if statement that prints "You can ride" when height is greater than or equal to 48.
One correct answer is: if height >= 48: print("You can ride") This works because the print statement only runs when height is at least 48. - 3
Trace this code: temperature = 68 if temperature > 80: clothing = "shorts" elif temperature > 60: clothing = "light jacket" else: clothing = "coat" What is the value of clothing? Explain why.
In an if/elif/else chain, Python checks each condition from top to bottom and stops at the first True condition.
The value of clothing is "light jacket" because 68 is not greater than 80, but it is greater than 60. - 4
What is the difference between if and elif in Python?
Think about the order Python uses when it checks conditions.
An if statement starts a conditional. An elif statement means "else if" and checks another condition only if the earlier if or elif conditions were False. - 5
Fix the indentation error in this code: score = 95 if score >= 90: print("Great job") else: print("Keep practicing")
The corrected code is: score = 95 if score >= 90: print("Great job") else: print("Keep practicing") The print statements must be indented because they belong inside the if and else blocks. - 6
Trace this code: coins = 10 if coins > 20: prize = "large" elif coins > 5: prize = "small" elif coins > 0: prize = "sticker" else: prize = "none" What is the value of prize? Explain why.
Only one branch in this chain will run.
The value of prize is "small" because 10 is not greater than 20, but 10 is greater than 5, so that elif block runs. - 7
Write a conditional that sets status to "passing" if grade is 60 or higher. Otherwise, it should set status to "not passing".
One correct answer is: if grade >= 60: status = "passing" else: status = "not passing" This sets status based on whether grade is at least 60. - 8
Trace this code: number = 7 if number % 2 == 0: result = "even" else: result = "odd" What is the value of result? Explain how the % operator is used here.
A number is even if dividing by 2 leaves a remainder of 0.
The value of result is "odd" because 7 % 2 leaves a remainder of 1, not 0. The % operator gives the remainder after division. - 9
A game gives a player a bonus if both conditions are true: the player has at least 100 points and has collected a key. Write an if statement using and that prints "Bonus unlocked".
The and operator requires both sides of the condition to be True.
One correct answer is: if points >= 100 and has_key == True: print("Bonus unlocked") This works because both conditions must be True for the message to print. - 10
Trace this code: has_ticket = False is_vip = True if has_ticket or is_vip: entry = "allowed" else: entry = "denied" What is the value of entry? Explain why.
For or, the whole condition is True if at least one part is True.
The value of entry is "allowed" because the or operator only needs one condition to be True, and is_vip is True. - 11
Rewrite this code using elif so that only one message can print: if score >= 90: print("A") if score >= 80: print("B") if score >= 70: print("C") else: print("Try again")
A correct version is: if score >= 90: print("A") elif score >= 80: print("B") elif score >= 70: print("C") else: print("Try again") Using elif makes Python stop after the first True condition. - 12
Trace this code: score = 92 if score >= 70: print("Passed") elif score >= 90: print("Excellent") else: print("Needs work") What prints? Explain why this may not give the message the programmer expected.
Order matters in an if/elif/else chain.
"Passed" prints because score >= 70 is the first True condition. The score is also at least 90, but Python never reaches that elif because it stops after the first True branch. - 13
Put these conditions in the best order for a grade checker: score >= 90, score >= 70, score >= 80. Explain your choice.
The best order is score >= 90, then score >= 80, then score >= 70. The highest scores should be checked first so that a score like 95 is not caught by a lower condition first. - 14
Write a conditional for a weather app. If rain_chance is 70 or higher, set advice to "Bring an umbrella". If rain_chance is 30 or higher, set advice to "Maybe bring an umbrella". Otherwise, set advice to "No umbrella needed".
Start with the most specific or highest range first.
One correct answer is: if rain_chance >= 70: advice = "Bring an umbrella" elif rain_chance >= 30: advice = "Maybe bring an umbrella" else: advice = "No umbrella needed" This checks the highest rain chance first and then handles the other cases. - 15
Find the bug in this code and explain how to fix it: password = "robot" if password = "robot": print("Access granted") else: print("Access denied")
Use == when asking whether two values are equal.
The bug is that the condition uses = instead of ==. In Python, = assigns a value, but == compares two values. The fixed condition is if password == "robot":.