Skip to content
Tactiverge

School/△ Builder/Week 5

Decisions from measurements: if, elif, else

A battery charger must refuse to charge below 0 °C and above 45 °C. How does a program choose between three actions?

About 35 min · 3 goals · Checked by automated tests

1.Why should I care?

Every responsive machine makes decisions: heater on or off, charge or wait, sort left or right. In code, each decision is an if statement built from a measurement and a threshold. Get the order or the boundary wrong and the machine misbehaves exactly at its limits.

  • ◇ Use if / elif / else to choose one action from a measured value.
  • ◇ Explain that only the first true branch runs, so the order of conditions matters.
  • ◇ Use comparison operators (<, <=, >, >=, ==, !=) and indentation correctly.

2.Discover

Choose Version B. Move the temperature slider and watch which return line lights up. Find the exact temperatures where the answer changes. Then switch to Versions A and C: do they ever disagree with the specification?

Loading the interactive…

Can't use the interactive? Read a description instead

The boundary tester shows the charge_mode function in three versions and a temperature slider from −10 to 60 °C in 0.5 °C steps. It highlights the line that returns and compares the result with the specification (charge only from 0 °C to 45 °C inclusive). Version B matches everywhere; Version A wrongly blocks at exactly 0 °C; Version C wrongly blocks at exactly 45 °C.

3.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1 · a fresh version every time

A cooling fan is set by: if temp_c >= 45: fan = 100, elif temp_c >= 35: fan = 50, else: fan = 0. What is fan, in %, when temp_c = 44.5?

%

Practice 2

A rule is written: if temp_c >= 35: fan = 50, elif temp_c >= 50: fan = 100. What is wrong?

4.Predict

In the charge_mode function, what does charge_mode(0) return?

Guess first. Being wrong here is how the surprise works.

5.Understand

The shape of a decision

def charge_mode(temp_c):
    if temp_c < 0:
        return "BLOCK: too cold"
    elif temp_c <= 45:
        return "CHARGE"
    else:
        return "BLOCK: too hot"

print(charge_mode(-3))    # BLOCK: too cold
print(charge_mode(0))     # CHARGE
print(charge_mode(45.5))  # BLOCK: too hot

Only the first true branch runs

Python checks the if, then each elif in order, and runs the block under the first condition that is True, then skips the rest. else catches everything left. So the cold check comes first, and the elif temp_c <= 45 only needs to handle temperatures that are already known to be 0 or above.

Colons, indentation and comparisons

Each if, elif and else line ends with :, and the lines that belong to it are indented (4 spaces). Comparisons give a bool: <, <=, >, >=, == (equal) and != (not equal). = stores a value and cannot be used in a condition.

Explain it another way

It is a set of doors tried in order. You walk through the first one that opens and never try the others.

Worked example 1

Write a fan rule: 100 % at 50 °C or more, 50 % from 35 °C up to 50 °C, otherwise off. What is the fan at 35 °C and at 49.5 °C?

if temp_c >= 50: fan = 100 / elif temp_c >= 35: fan = 50 / else: fan = 0. At 35: the first test is False, the second True, so 50 %. At 49.5: also 50 %. Order matters: putting the 35 test first would give 50 % even at 80 °C.

Common mix-ups

  • All the branches whose conditions are true will run.
    ✓ Only the first true branch runs; the rest are skipped.
  • Indentation is just for looks.
    ✓ In Python, indentation decides which lines belong to which branch.

6.Watch

→ Watch how elif and else attach to an if, and notice the colon and indentation on every branch.

Walks through if, elif and else, comparisons and the Boolean operators and, or and not.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

The video is optional: everything you need is also in the lesson text and the interactive.

7.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 3

Which condition tests whether a reading equals the limit?

Practice 4

When does the else block in charge_mode run?

8.Apply it

Write classify(level_pct) for a water tank: 'LOW' below 20 %, 'OK' from 20 % to 90 % inclusive, 'HIGH' above 90 %. Print it for 0, 19.5, 20, 90 and 90.5 and check each by hand.

9.Reflect

Concepts: Decisions and loops