انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Python: home
0. Lab tour1
1. The start5
2. Variables and input5
3. Conditions5
4. Loops5
5. Lists4
6. Functions4
7. Drawing with the turtle3
8. The project: the guess-the-number game3
9. Strings in depth1
10. Dictionaries (dict)3
11. Randomness and errors3
12. Classes and objects (class)2
13. Pro drawing and charts3
14. The final project: the quiz game2

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, and round(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:
Ali 80.0
Mona 93.3
Omar 75.0
The best: Mona
🐍 Open the code in Robo Python ↗
The report card in Robo Python
The report card in Robo Python
  1. Add a fourth student with higher marks than Mona, and predict who will be first.
  2. 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.

🐍 Only those who passed
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:
Ali passed with 80.0
Mona passed with 93.3
🐍 Open the code in Robo Python ↗
✍️ Practise
Try the lesson's examples here: copy the code, change it, and click ▶ Run.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

Print Mona's mark in the second exam.

👀 Show the solution
🐍 Solution
grades = {"Ali": [80, 90, 70], "Mona": [95, 85, 100]}
print(grades["Mona"][1])
🖥️ The output in Robo Python:
85
🐍 Open the code in Robo Python ↗

Result: 85

Medium

Add a new student and their marks, then print the number of students.

👀 Show the solution
🐍 Solution
grades = {"Ali": [80, 90, 70], "Mona": [95, 85, 100]}
grades["Sara"] = [88, 92, 79]
print(len(grades))
🖥️ The output in Robo Python:
3
🐍 Open the code in Robo Python ↗

Result: 3

Hard

The highest mark in the whole class, and whose it is.

👀 Show the solution
🐍 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:
Mona 100
🐍 Open the code in Robo Python ↗

Result: Mona 100

Finished the lesson? Mark it so your progress counts.