Python Basics · Lesson 12 of 12

Project: A Score Report

For your final project you write a small data program: the kind of thing Python is used for every day. It takes the quiz scores of a class and reports on them.

You are given a dictionary that maps each student's name to a list of their scores:

scores = {
    "Ana": [82, 91, 77],
    "Ben": [58, 64, 70],
    "Cleo": [95, 88, 99],
}

Notice the values are lists. scores["Ana"] is the list [82, 91, 77].

The plan

  1. Write a function average that takes a list of numbers and returns their average. You know the ingredients: sum() and len().
  2. Loop over the dictionary with .items(). For each student, work out the average with your function and print one line of the report.
  3. While looping, keep track of the best average seen so far and who has it. This is the "build up a result" pattern from the loops lessons: start with a low value before the loop, and replace it whenever you find a higher one.

Formatting numbers

In an f-string, :.1f after a value shows it with exactly one decimal:

value = 83.3333
print(f"{value:.1f}")   # 83.3

Take it step by step

Write the function first and test it with print(average([1, 2, 3])), which should show 2.0. Then write the loop. Run your code often.

When the checks pass you have finished the course. Your certificate is on the course page.

Your Task

  1. Write a function average(numbers) that returns the average of a list of numbers.
  2. Loop over scores and print one line per student in the form Ana: 83.3 (the average with one decimal).
  3. Find the student with the highest average and store their name in best.
  4. After the loop, print Top student: followed by the name.
  • average returns the average of any list of numbers
  • The report has one line per student with the average to one decimal
  • best holds the name of the student with the highest average
  • The last line printed is Top student: Cleo
  • The program uses a loop over the dictionary, not three hand-written lines