if / elif / else and what counts as true

Late-fee bands must not overlap. == asks; = stores. Empty lists and 0 are the usual ‘no’ in Python; a clerk-facing function should still return an explicit 0 or "ok".

Junction traffic light

if / elif / else choose a path.

You will be able to

  • Write exclusive if/elif/else bands
  • Use == to ask and = to store
  • Know empty/0 as false on a laptop, still return explicit 0 at the edge

Exclusive bands

elif is the next exclusive band. Three separate ifs can double-charge. else is everything left, including on-time. Return 0 for on-time so callers add fees without a special branch. A ternary x if c else y exists on a laptop; an if statement is clearer for clerks.

== asks; = stores. Comparisons < > <= >= != complete the set. Nested ifs stand in for and/or in this sandbox.

Truth tests

On a laptop, empty lists and 0 are false in if x:. Do not rely on that at a kiosk — print "ok" or return 0. Multiway branching is if/elif, not a C switch.

Words that matter

elif
Next exclusive band after a failed if.
==
Ask whether two values are equal.
Truthy
Laptop: non-zero / non-empty counts as true in if x.

Common mistakes

Avoid: Three separate ifs for overlapping fee bands.

Do this: if / elif / else so a day count hits one band.

Run it step by step

Each box is a real program. Press Run, change a number, Run again — the output must follow your code.

1. Step 1 — a gate

Change age to 17 and Run again.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

2. Step 2 — exclusive bands

4 days should print Mid, not two fees.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Example program — Late fee as a number

On time is 0.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Your turn — Overtime flag

hours = 10. Print OT if hours > 8, else print Regular.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Self-assessment

Check your understanding before you mark the lesson complete.

1. To store 10 in fee you write…
2. The on-time late fee should usually be…

Progress is stored in a browser cookie on this device.