Course 5.3Lesson 3 of 4
A grade book turns raw marks into a report. The names and the marks are in two lists with the same position for the same student, and two small functions do the thinking: grade gives a letter for a mark, and average gives the mean of a list. A loop prints a line for each student with the columns lined up by f-string widths, then the class average and the highest mark, and marks.index finds the position of the highest mark, so names at that position is the name. Test the grade function at the edges. Extend the grade book by counting how many students got each grade.
Try the code and the simulations beside the lesson.
After this lesson you can
A grade book turns raw marks into a report.
grade(mark) gives a letter. average(numbers) gives an average. Small functions are easy to test.Write the expected answers first. For the five students Asha 72, Ravi 85, Meera 64, Kabir 90 and Ana 58: the grades are C, B, C, A and D (90 and more is A, 75 and more is B, 60 and more is C, and below 60 is D). The average is 369 divided by 5, which is 73.8. The highest mark is 90, by Kabir.
The edges of the grade function are 60, 75 and 90. Write a table of cases, and run a loop, as you learned in the course on functions.
| Mark | Expected |
|---|---|
| 100 | A |
| 90 | A |
| 89 | B |
| 75 | B |
| 74 | C |
| 60 | C |
| 59 | D |
| 0 | D |
If a student with exactly 75 gets a C, the fault is at the edge mark >= 75, and the table finds it at once.
A teacher wants to know how many students got each grade. This is a counting job with four counters. A neat way is a list of the letters, and a list of counts at the same positions.
The four bars always add up to 5, because every mark lands in exactly one group.
Pick one or two, and do them without looking at a solution. Write the expected answers first.
append on both lists, and show that the report changes.grade(average(marks)).IndexError.< and > mixed up in the f-string widths. < is left, > is right.print in the functions. You cannot use their answers.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
a_list.index(value) gives the position of a value.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
grade(85) in the grade book above?marks.index(90) give for [72, 85, 64, 90, 58]?{x:>4} do in an f-string?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.