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
- Write a function
averagethat takes a list of numbers and returns their average. You know the ingredients:sum()andlen(). - Loop over the dictionary with
.items(). For each student, work out the average with your function and print one line of the report. - 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
- Write a function
average(numbers)that returns the average of a list of numbers. - Loop over
scoresand print one line per student in the formAna: 83.3(the average with one decimal). - Find the student with the highest average and store their name in
best. - After the loop, print
Top student:followed by the name.
averagereturns the average of any list of numbers- The report has one line per student with the average to one decimal
bestholds 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
Lesson complete!
Get Your Certificate