...

/

Solution: Students Performance Tracking System

Solution: Students Performance Tracking System

Learn how to implement the coded solution of students’ performance tracking system using the Iterator design pattern.

Calculate averages

Create a function named generate_average_grades() to calculate the average grade of each student.

Press + to interact
# Create a list of dictionaries, each representing a student's performance
student_data = [
{
"name": "Alice",
"age": 18,
"subjects": {"Math", "English", "Science"},
"grades": {"Math": 85, "English": 92, "Science": 78}
},
{
"name": "Bob",
"age": 17,
"subjects": {"Math", "History", "Art"},
"grades": {"Math": 68, "History": 85, "Art": 60}
},
{
"name": "John",
"age": 19,
"subjects": {"Chemistry", "English", "Biology"},
"grades": {"Chemistry": 90, "English": 78, "Biology": 91}
},
{
"name": "Geene",
"age": 18,
"subjects": {"Chemistry", "Math", "Biology"},
"grades": {"Chemistry": 70, "Math": 72, "Biology": 61}
},
{
"name": "Fredrik",
"age": 20,
"subjects": {"Finance", "Chemistry", "Biology"},
"grades": {"Finance": 76, "Chemistry": 82, "Biology": 89}
},
{
"name": "Aliya",
"age": 17,
"subjects": {"History", "Physics", "English"},
"grades": {"History": 77, "Physics": 62, "English": 79}
},
{
"name": "Daniel",
"age": 19,
"subjects": {"Art", "Chemistry", "Biology"},
"grades": {"Art": 60, "Chemistry": 81, "Biology": 82}
},
{
"name": "Jay",
"age": 18,
"subjects": {"Physics", "Chemistry", "English"},
"grades": {"Physics": 79, "Chemistry": 85, "English": 67}
},
]
# Calculate averages
def calculate_average(grades):
total_score = sum(grades.values())
num_subjects = len(grades)
return total_score / num_subjects
def generate_average_grades(student_data):
for student in student_data:
name = student["name"]
grades = student["grades"]
if grades:
average_grade = calculate_average(grades)
yield (name, average_grade)

Code explanation

  • Line 55–58: Calculated the average grade from a given dictionary of grades. It calculated the total score by summing ...