Some decisions come in stages: first check the oven is hot enough, and only then ask whether the cooking time is long enough. So you write an if inside another if.
temperature = 180
cook_time = 12
if temperature >= 170:
print("Heating temperature OK")
if cook_time >= 12:
print("Cooking successful")Heating temperature OK Cooking successful
The inner condition is only checked if the outer one is true. Lines that belong to the inner if are indented twice (8 spaces).
else inside if
Inside the outer if you can write a full if / elif / else. The indentation decides which if each else belongs to:
weather = "sunny"
team = "Green Team"
if weather == "sunny":
if team == "Red Team":
print("Destination: the farm")
elif team == "Blue Team":
print("Destination: the science museum")
else:
print("Destination: the aquarium")
else:
print("Field trip canceled")Destination: the aquarium
| Part | Indent | Belongs to |
|---|---|---|
| inner else | 4 spaces | if team == ... |
| outer else | none | if weather == ... |
Counting inside a condition
A counter is a variable that starts at 0 and goes up by 1 each time a condition holds: count = count + 1. To print it with text, convert it with str():
bug = 45
rare_count = 0
if bug >= 45:
print("Got a Rare Beetle!")
rare_count = rare_count + 1
print("Rare Beetle Counter:" + str(rare_count))Got a Rare Beetle! Rare Beetle Counter:1
With several separate ifs, the counter counts every value that passes:
passed = 0
math = 72
science = 45
english = 90
if math >= 50:
passed = passed + 1
if science >= 50:
passed = passed + 1
if english >= 50:
passed = passed + 1
print("Subjects passed: " + str(passed))Subjects passed: 2
Practice
Inside the condition, add: if cook_time is 12 or more, print Cooking successful.
temperature = 180
cook_time = 12
if temperature >= 170:
print("Heating temperature OK")Press Run to see the result. Set score to 90 and rank to 1. If score is 85 or more, print Certification passed; inside that, if rank is 1, print Trophy earned.
Press Run to see the result. If the shop is not open (and it is sunny), print Buy snacks at the convenience store and go stargazing.
weather = "sunny"
shop = "closed"
if weather == "sunny":
if shop == "open":
print("Buy a star chart and go stargazing")
___
___
else:
print("Planetarium DVD at home")Press Run to see the result. Inside the sunny-weather branch: if team is "Red Team" print Destination: the farm, if it is "Blue Team" print Destination: the science museum, otherwise print Destination: the candy factory.
weather = "sunny"
team = "Green Team"
if weather == "sunny":
if team == "Red Team":
print("Destination: the farm")
else:
print("Field trip canceled")Press Run to see the result. If bug is 90 or more: print Super Rare Beetle Get!, add 1 to super_rare_count, then print "Super Rare Beetle Counter:" + str(super_rare_count).
bug = 95 super_rare_count = 0
Press Run to see the result. A student's result: total = 380 and absent_days = 4. If total is 250 or more: if absent days are 10 or fewer print Passed, otherwise print Passed, but too many absences. If total is under 250, print Second round exam.
total = 380 absent_days = 4
Press Run to see the result. A team played three matches: match1 = "win", match2 = "loss", match3 = "win". Start wins = 0, add 1 for each match equal to "win" (three ifs), then print Wins: 2.
match1 = "win" match2 = "loss" match3 = "win"
Press Run to see the result. Key points
- if inside if: the inner condition is checked only when the outer one is true.
- Indentation decides which if each else or elif belongs to.
- A counter starts at 0 and grows with
count = count + 1inside a condition; print it with text usingstr().