if, elif and else
Write and trace an if, elif and else chain whose conditions are ordered so that each input reaches the one block that is meant for it.
It opens on
With score = 85, will the program on the board print A, B, C, or more than one of them?
Why it matters
Programs make decisions with if statements, such as which grade, which price or which error message to give, and the commonest bug is a chain whose conditions are in the wrong order or are separate ifs, which gives a wrong answer only for some inputs.
Good to know first
- Booleans and comparisons: score >= 80 gives True or False, and the cutoff itself counts with >=
- Variables and assignment: grade = "B" binds the name grade to a str
Step by step
One block of a chain runs
Trace an if, elif and else chain for a score and say which single block runs and what is printed.
With score 85, the first test 85 >= 90 is False, so its block is skipped. The second test 85 >= 80 is True, so grade is bound to "B", and Python skips the rest of the chain and goes on to print, which shows B.
A common mix-up
Python tests every condition and runs every block whose condition is True.
In fact: The chain stops at the first True condition, so for 85 only the B block runs.
The order of the conditions
Say why the order of conditions in an elif chain matters, order a chain of cutoffs correctly, and test it on the cutoffs.
The first test is 95 >= 70, which is True, so grade is bound to "C", the chain stops, and the tests for 80 and 90 are never reached; the program prints C for a score of 95.
A common mix-up
The order of the conditions does not matter, since each is a correct test.
In fact: The first True condition ends the chain, so a test that catches too many scores must come after the stricter ones.
if against elif
Say which blocks run for separate ifs and for an if-elif chain, and make sure that every input gets a value.
Grade is F to start with. The first if tests 95 >= 90, which is True, so grade is bound to "A". The second if is a new statement, so it is tested whatever happened above it, and 95 >= 80 is True, so grade is now "B".
A common mix-up
Every if in a program belongs to one chain, so only one block can run.
In fact: Only an elif or else joins the if above it, and each separate if is tested on its own.
Try it
What does the program print for score = 79?Answer
C
Hint: 79 >= 80 is False, 79 >= 70 is True
Put these three tests in the right order for a chain, score >= 70, score >= 90 and score >= 80.Answer
Score >= 90, then score >= 80, then score >= 70
Hint: The strictest first
With n = 8, what does if n > 5: n = n + 1 followed by a separate if n > 8: n = n * 2 give for print(n)?Answer
18
Hint: The second test sees the new n, 9
For score 85, how many tests does Python work out, and why does it stop?Answer
Two, because the second test is True and the rest of the chain is skipped, even though 85 >= 70 would be True.
With this order, which scores can ever get the grade B?Answer
None, because every score of 80 or more is also 70 or more, so the first test catches it.
