Python / Making decisions

Nested conditions and counting

Put an if inside another if, tell which else belongs where from the indentation, and increase a counter inside a condition. Baccalaureate

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.

Python
temperature = 180
cook_time = 12
if temperature >= 170:
    print("Heating temperature OK")
    if cook_time >= 12:
        print("Cooking successful")
Console
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:

Python
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")
Console
Destination: the aquarium
PartIndentBelongs to
inner else4 spacesif team == ...
outer elsenoneif weather == ...
Classic mistake: writing the inner else with no indentation. It then belongs to the outer if, and the program means something completely different.

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():

Python
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))
Console
Got a Rare Beetle!
Rare Beetle Counter:1

With several separate ifs, the counter counts every value that passes:

Python
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))
Console
Subjects passed: 2

Practice

1Edit the programCurriculum practice

Inside the condition, add: if cook_time is 12 or more, print Cooking successful.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
temperature = 180
cook_time = 12
if temperature >= 170:
    print("Heating temperature OK")
Console
Press Run to see the result.
2Write the programCurriculum practice

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.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
3Fill in the blankCurriculum practice

If the shop is not open (and it is sunny), print Buy snacks at the convenience store and go stargazing.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
weather = "sunny"
shop = "closed"
if weather == "sunny":
    if shop == "open":
        print("Buy a star chart and go stargazing")
    ___
        ___
else:
    print("Planetarium DVD at home")
Console
Press Run to see the result.
4Edit the programCurriculum practice

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.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
weather = "sunny"
team = "Green Team"
if weather == "sunny":
    if team == "Red Team":
        print("Destination: the farm")
else:
    print("Field trip canceled")
Console
Press Run to see the result.
5Edit the programCurriculum practice

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).

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
bug = 95
super_rare_count = 0
Console
Press Run to see the result.
6Write the programExtra practice

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.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
total = 380
absent_days = 4
Console
Press Run to see the result.
7Write the programExtra practice

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.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
match1 = "win"
match2 = "loss"
match3 = "win"
Console
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 + 1 inside a condition; print it with text using str().