Problem overview
Version 4 grades a whole class instead of one score. Two lists are given: names holds the students and scores holds their marks, and position i in both lists belongs to the same student. For each student the program prints the name, a colon, a space and the letter grade, using the scale from version 3: 90 and above is an A, 80 to 89 a B, 70 to 79 a C, 60 to 69 a D and below 60 an F.
With the starter data the output is:
Alice: A
Bob: C
Carol: B
Dan: D
This time the program prints the lines itself, one per student.
Approach: loop over positions, grade each score
The grading ladder from version 3 stays as it is. What changes is that it now runs once per student, so it moves inside a loop. The loop needs the name and the score from the same position, so it counts positions, 0 to the last one, and uses each position to read both lists.
range(len(names)) produces exactly those positions: 0, 1, 2, 3 for four students. At each position, the score comes from scores[i] and the name from names[i].
Step by step
- Write the loop
for i in range(len(names)):. Everything that follows is indented inside it. - Read the score into
score = scores[i]. This gives the ladder a short name to test. - Start the ladder with
if score >= 90:and setgrade = "A". The highest grade comes first, so each later test needs only a lower limit. - Add
elif score >= 80:for"B". - Add
elif score >= 70:for"C". - Add
elif score >= 60:for"D". - End with
else:for"F". Nowgradegets a value on every pass. - Print the line after the ladder, still inside the loop, so it runs once per student: the name,
": "andgrade.
The solution in Python
names = ["Alice", "Bob", "Carol", "Dan"]
scores = [92, 78, 85, 67]
for i in range(len(names)):
score = scores[i]
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
print(names[i] + ": " + grade)
A few details about this code:
- Both parts of the line are strings.
names[i],": "andgradeare all text, so+joins them. A number would have to go throughstr()first. - The indentation decides what repeats. The
printis indented at the same level as theif, so it runs once per student. If it were moved out to the left margin, it would run once, after the loop, and print only the last student's line. - Variables carry over between passes.
scoreandgradeare overwritten on every pass. That is safe here because the ladder always assignsgrade.
Tracing the first two students
On the first pass, i is 0, so score becomes 92. The test 92 >= 90 is true, grade becomes "A", and the program prints Alice: A. The remaining branches are skipped.
On the second pass, i is 1 and score is 78. The first two tests fail, because 78 is below 90 and below 80. The test 78 >= 70 succeeds, so grade becomes "C" and the program prints Bob: C. Carol (85) lands in the B branch and Dan (67) in the D branch in the same way.
Looping over positions or over the lists directly
The lesson counts positions because one number, i, can then read both lists. That is a pattern worth knowing, since it works whenever several lists are parallel. The scores are a plain list of numbers, so you can't loop over them and also learn whose score each is. Looping over names alone has the opposite problem.
Python also offers zip, which walks several lists side by side: for name, score in zip(names, scores): gives each name together with its score, and it stops at the shorter list instead of raising an error. The lesson's steps use range(len(names)), and the solution above is written that way.
Edge cases and common mistakes
- Lists of different lengths. The loop counts positions of
names. Ifscoreswere shorter, for instance one score for two names, the first student would print normally and the second would stop the program with anIndexError: list index out of range. Ifscoreswere longer, the extra scores would be ignored without any message. - Boundary scores. Scores of 90, 59 and 60 give A, F and D, because each limit uses
>=. - An empty class. With empty lists,
range(0)has no positions, so the loop body never runs and nothing is printed. - Leaving out the
else. Without it, a failing score would not assigngradeat all. On the first pass that raises aNameError, but on a later pass the variable still holds the previous student's grade, so a failing student would be printed with someone else's letter and no error at all. Ending the ladder withelseremoves the problem. - Printing outside the loop. This is the indentation mistake described above.
Testing
Another class worth trying is names = ["Eve", "Finn", "Gus"] with scores = [90, 59, 60]. It prints:
Eve: A
Finn: F
Gus: D
