Two doors isn’t always enough. Today: how to sort a value into many buckets with elif, and how to read conditionals inside conditionals — a top-three hardest question type on the exam, so we start the drill now.
Shows why a chain of elif beats a pile of separate ifs.
Enrolled in our Runestone course? Open it from there so your progress counts.
Still in Programs. In 2.12 an if chose between two
paths. Real problems often need many paths — a letter grade has five, a shipping
speed has three. elif is how you chain conditions so exactly one wins.
That “check in order, stop at the first
match” behavior is exactly what an elif chain does — and it’s why
a chain beats a stack of separate ifs, which would keep checking and can fire more
than once.
elif is short for “else if.” Python checks each condition top to bottom
and runs the block for the first one that is True — then skips the entire
rest of the chain. An optional else at the end catches everything that matched
nothing.
if
statements instead of a chain, a score of 95 is >= 90 and
>= 80 and >= 70… so it can print several grades.
An elif chain stops at the first match, so it prints exactly one. Order matters:
highest threshold first.
score = 85 if score >= 90: print("A") elif score >= 80: print("B") elif score >= 70: print("C") else: print("D or F")
score ← 85 IF (score ≥ 90) { DISPLAY("A") } ELSE IF (score ≥ 80) { DISPLAY("B") } ELSE IF (score ≥ 70) { DISPLAY("C") } ELSE { DISPLAY("D or F") }
Hover or tap a line to light its twin. Trace score = 85:
85 >= 90? No. 85 >= 80? Yes — print B and stop.
The >= 70 check never even runs.
Nesting is a conditional inside another conditional’s block. You only ask the inner question if the outer one already passed — like “is it raining? if so, do I have an umbrella?” The umbrella question is meaningless unless it’s raining.
n = 7 if n > 0: if n % 2 == 0: print("positive even") else: print("positive odd") else: print("not positive")
# outer question: is n positive? # if yes, ask the inner question # inner: is it even? # deepest indent = both True # # positive but odd # outer else: n was 0 or negative # inner question never asked
For n = 7: positive? yes → even? no → prints
positive odd. The indentation is the whole story — count the levels.
Enter a score and watch the elif chain check conditions top to bottom, stopping at
the first match. The greyed-out rows are the checks that never run.
Five traces. Use a trace table — one variable, each condition marked True or False, follow only the matching block. Then pick your answer.
An elif chain doesn’t just compute an answer
— the order of its conditions decides outcomes. A benefits program that checks
“income under $20k?” before “income under $40k?” hands out different aid
than one that checks them in the other order. Same conditions, different order, different lives
affected.
This is a quiet but real source of software bugs and unfairness: reorder the branches and you change who falls into which bucket. When you write a chain — or read someone else’s — the order is a design decision worth questioning, not just a formatting detail.
if/else had two paths;
elif stretches that to as many as you need.and, or, and not.elif chain over
age brackets, exactly like today’s grade classifier.