Lite / Building Machines That Respond / 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?
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.
Predict
In the charge_mode function, what does charge_mode(0) return?
- BLOCK: too cold
- CHARGE
- BLOCK: too hot
Guess first, then reveal
CHARGE. 0 < 0 is False, so Python moves to elif: 0 <= 45 is True, so it returns CHARGE. The specification includes 0 °C in the charging range.
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?
What the interactive shows (text version)
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.
Watch (optional)
Watch for: 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.
“Python Tutorial for Beginners 6: Conditionals and Booleans - If, Else, and Elif Statements” by Corey Schafer (opens YouTube; streaming video uses a lot of data)
The video is optional. Everything you need is in this lesson's text.
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 hotOnly 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
Intuitive
It is a set of doors tried in order. You walk through the first one that opens and never try the others.
Technical
An if/elif/else chain is evaluated top to bottom; each condition is converted to bool, and the first truthy branch executes. Indentation defines the block structure in Python.
Applied
Lithium-ion battery management systems really do block charging outside a temperature window (a common one is 0–45 °C, but each cell's datasheet sets its own), because charging a cold cell can damage it permanently.
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.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
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.
Reflect
Why do you think safety checks are usually written first in an if/elif chain?
Write your answer on paper or in your own notes. Lite never stores what you write.