Project: grade book report

Mini Projects, lesson 6 of 6

🚀 Mini Projects, lesson 6 of 6

Project: grade book report

Parse raw scores, compute averages and letter grades, and print a ranked report.

12 min

3 exercises

1 quiz

0/4 solved

Getting Python ready… examples can run in a moment.

Teachers often get scores as plain text, one student per line: Ana, 92, 88, 95. You'll turn that into a ranked report:

  1. Parse the text into a dict of score lists
  2. Average and letter grade helpers
  3. Report: a ranked, aligned table with a class average
Example

Unpacking with a star

*scores collects "everything else" into a list, and int() ignores the spaces around each number.

Exercise

Step 1: parse the scores

Write parse_scores(text) that returns a dict mapping each (stripped) name to a list of int scores. Skip blank lines.

Exercise

Step 2: averages and letters

  • average(scores) returns the mean rounded to 1 decimal, or 0.0 for an empty list
  • letter(avg) returns "A" for 90+, "B" for 80+, "C" for 70+, "D" for 60+, else "F"

Step 3: the report. Sort students by average, highest first, and line the columns up with f-string widths: {name:<8} pads to 8 characters (left-aligned) and {avg:>5.1f} right-aligns a number with 1 decimal in 5 characters.

Quiz

What does this print?

Example

Sorting by a computed value

sorted on a dict sorts its keys, ordered by whatever the key function returns. Remove reverse=True to list the lowest first.

Exercise

Step 3: the ranked report

Write report(grades), returning one line per student, highest average first, formatted as f"{name:<8}{avg:>5.1f} {grade}", then a final line Class average: <x> (the average of the student averages). For RAW it looks like:

Ana      91.7  A
Dee      84.3  B
Ben      73.0  C
Cy       65.0  D
Class average: 78.5