Five outcomes, not two
Problem 10 had one condition and two answers. A grade has five, and they are not independent — a student with 95 is also above 75 and above 60 and above 40. Every band except the top one is true for them. Something has to decide which band actually applies.
That single word is what turns a pile of separate questions into one decision with exactly one answer. Python works down the chain, stops at the first condition that is true, runs that block, and skips everything else — including the
else.Why the order is the whole problem
Written in the wrong order the chain still runs, still produces a grade for every student, and is still wrong:
A student with 95 meets the first test, so they are given a D and the chain stops. The line awarding an A is never reached for anybody — it sits there looking perfectly correct. There is no error, no warning, and the programme produces a full set of grades that a spreadsheet would accept without complaint.
Test the narrowest band first. Work downwards, hardest to easiest, and let else catch whatever is left.
elif with if and you have quietly split one decision into two separate ones. Both get tested, both can run, and the second overwrites the grade the first just awarded. The programme becomes a machine that assigns the grade of the last matching band instead of the first.The bands
| Marks | 90 and above | 75 to 89 | 60 to 74 | 40 to 59 | below 40 |
|---|---|---|---|---|---|
| Grade | A | B | C | D | F |
Notice that only the lower edge of each band is ever written down. The upper edge looks after itself: if a student is not in the 90 band, they cannot be above 90, so the 75 test does not need to say so.
What you are building
and and or, which is problem 12.Build the programme
The top band is already in place. Put the other three bands, and the catch-all, into an order that grades every student correctly.
Every one of the four orders you could choose will run without an error. Only one of them is right, so reason it out rather than trying them: which band would swallow students who belong somewhere else?
Your plan
The steps still to place
Your chain written out in full. Five pieces are missing. One of them is a single word that looks harmless and changes what the whole programme means.
Leave nothing on "choose…". A wrong pick does not always cause an error — sometimes it just prints something you did not expect, which is the harder kind of mistake to spot.
Run and read
Here is the whole programme. Change the marks and run it again — and make sure one of your runs is a student sitting exactly on a band edge, at 90, 75, 60 or 40.