Project: the report card
Section 10: Dictionaries (dict)Lesson 10.3 · ⏱ 12 min
🎯 Project goal
Every student has a list of marks. The program works out each student's average and announces the top one.
- A value in a dictionary can be a whole list.
grades.items()gives us the name and the value together in every round.sum(marks) / len(marks)is the average, andround(avg, 1)rounds it to one decimal place.- We keep the best average we have seen so far in
best_avg, and change it whenever we find a bigger one.
grades = {"Ali": [80, 90, 70], "Mona": [95, 85, 100], "Omar": [60, 75, 90]}
best = ""
best_avg = 0
for name, marks in grades.items():
avg = sum(marks) / len(marks)
print(name, round(avg, 1))
if avg > best_avg:
best, best_avg = name, avg
print("The best:", best)
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Ali 80.0 Mona 93.3 Omar 75.0 The best: Mona

- Add a fourth student with higher marks than Mona, and predict who will be first.
- Print “pass” next to each student whose average is 50 or more.
📌 Another example: only those who passed
We print the names of those whose average is 75 or more.
grades = {"Ali": [80, 90, 70], "Mona": [95, 85, 100], "Omar": [60, 75, 70]}
for name, marks in grades.items():
avg = sum(marks) / len(marks)
if avg >= 75:
print(name, "passed with", round(avg, 1))
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Ali passed with 80.0 Mona passed with 93.3
✍️ Practise
Try the lesson's examples here: copy the code, change it, and click ▶ Run.
✍️ Practise here in the lesson✖ Close the labor open the lab in a new page ↗
🎯 Challenge yourself
Easy
Print Mona's mark in the second exam.
👀 Show the solution🙈 Hide the solution
grades = {"Ali": [80, 90, 70], "Mona": [95, 85, 100]}
print(grades["Mona"][1])
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗85
Result: 85
Medium
Add a new student and their marks, then print the number of students.
👀 Show the solution🙈 Hide the solution
grades = {"Ali": [80, 90, 70], "Mona": [95, 85, 100]}
grades["Sara"] = [88, 92, 79]
print(len(grades))
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗3
Result: 3
Hard
The highest mark in the whole class, and whose it is.
👀 Show the solution🙈 Hide the solution
grades = {"Ali": [80, 90, 70], "Mona": [95, 85, 100], "Omar": [60, 75, 99]}
best, who = 0, ""
for name, marks in grades.items():
if max(marks) > best:
best, who = max(marks), name
print(who, best)
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Mona 100
Result: Mona 100

