← Back to course
Code Crew G6+ / Week 03 / Wednesday
3/6
Week 03 Β· Decisions

Wednesday

More than two choices
// if, elif, else and the logic words
⏱ about 30 min

Wednesday: More than two choices

The fuel gauge in Mission Control has four colors, not two. Green means ready, yellow means top up soon, orange means refuel before launch, and red means stop everything.

"My program only knows yes and no," Rocket says. "I need four answers."

Raven draws the gauge as a ladder with four steps. "What do you notice?" she asks. "We check from the top step down. The first step that fits is the answer."

Nova lights up each step in turn, top to bottom. "Interesting," she says. "Python has a word for else if. It checks the next condition only when the ones above it were False."

Rocket adds the middle steps to his program, carefully, from the top down.

elif

elif is short for else if. Python checks the conditions from top to bottom and runs the block of the first one that is True. It skips all the rest, even if they are True too.

The else at the end catches everything that did not match. You can have as many elif parts as you need.

# fuel_gauge.py
fuel = int(input("Fuel percent: "))
if fuel >= 80:
    print("Green: ready")
elif fuel >= 50:
    print("Yellow: top up soon")
elif fuel >= 20:
    print("Orange: refuel before launch")
else:
    print("Red: do not launch")
Fuel percent: 65
Yellow: top up soon
READ THE GAUGE
  • Read the question.
  • Tap your answer.
The user types 20. What does it show?
The user types 5. What does it show?
The user types 95. How many lines after the question does it show?

Order matters

Here the steps are in the wrong order. The first condition catches almost everything, so the program gives the wrong color.

# gauge_bug.py (conditions in the wrong order)
fuel = 90
if fuel >= 20:
    print("Orange")
elif fuel >= 80:
    print("Green")
What does gauge_bug.py show?
WHY THIS EXERCISEThe first condition is True, so Python runs that block and skips the rest. Put the biggest threshold first.
At your computer
1. Type fuel_gauge.py in a new file and save it.
2. Run it four times with 95, 65, 20 and 5. Check each color.
3. Try 50 and 49. These are the edge cases, right on and just under a line.
4. Swap the first two conditions and their prints. Run with 95 and see the bug.
5. Put them back in the right order.

Practice: wind levels

Here is another ladder of conditions. This time the thresholds go up, so the program checks the smallest one first with less than.

# wind_level.py
wind = int(input("Wind speed in km per hour: "))
if wind < 20:
    print("Calm")
elif wind < 40:
    print("Breezy")
elif wind < 60:
    print("Windy")
else:
    print("Too windy to launch")
Wind speed in km per hour: 40
Windy
CLIMB THE LADDER
  • Read the question.
  • Tap your answer.
Why does 40 show Windy and not Breezy?
The user types 75. What does it show?
The user types 19. What does it show?
The user types 20. What does it show?
The user types 59. What does it show?
The user types 60. What does it show? (first word)
StatementTrue or false?
A chain of if, elif and else runs at most one block.?
You can have only one elif in a chain.?
If no condition is True and there is no else, nothing in the chain runs.?
Python checks every elif even after one is True.?
WHY THIS EXERCISEPython runs the first True block, skips the rest, and the else only runs when nothing above matched.

Testing the values right on each line, like 80, 50 and 20, is how programmers catch mistakes. Tomorrow you will combine conditions with and, or and not.

← Tuesday