JIT — Intro to Data Science · Python Lab · Problem 11 of 30

Grade bands

Five outcomes from one number — where writing the conditions in the wrong order produces a full set of grades that are quietly all wrong.

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.

New word — elif Short for "else if". It means: only bother testing this if every test above it has already failed.
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:

if marks >= 40: # tested first grade = "D" elif marks >= 90: grade = "A"

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.

Careful — elif and if are not interchangeable Replace one 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

Marks90 and above75 to 8960 to 7440 to 59below 40
GradeABCDF

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

Marks: 78 Grade: B
Coming later Here each band depends on one number. Deciding on two things at once — marks and attendance — needs 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.

Your programme

Output

Nothing has run yet.