🚀 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:
- Parse the text into a dict of score lists
- Average and letter grade helpers
- Report: a ranked, aligned table with a class average
Unpacking with a star
*scores collects "everything else" into a list,
and int() ignores the spaces around each number.
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.
Step 2: averages and letters
average(scores)returns the mean rounded to 1 decimal, or0.0for an empty listletter(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.
What does this print?
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.
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