| 109 | |
| 110 | print("Example 5") |
| 111 | class WeightedGradebook: |
| 112 | def __init__(self): |
| 113 | self._grades = {} |
| 114 | |
| 115 | def add_student(self, name): |
| 116 | self._grades[name] = defaultdict(list) |
| 117 | |
| 118 | def report_grade(self, name, subject, score, weight): |
| 119 | by_subject = self._grades[name] |
| 120 | grade_list = by_subject[subject] |
| 121 | grade_list.append((score, weight)) # Changed |
| 122 | |
| 123 | def average_grade(self, name): |
| 124 | by_subject = self._grades[name] |
| 125 | |
| 126 | score_sum, score_count = 0, 0 |
| 127 | for scores in by_subject.values(): |
| 128 | subject_avg, total_weight = 0, 0 |
| 129 | for score, weight in scores: # Added inner loop |
| 130 | subject_avg += score * weight |
| 131 | total_weight += weight |
| 132 | |
| 133 | score_sum += subject_avg / total_weight |
| 134 | score_count += 1 |
| 135 | |
| 136 | return score_sum / score_count |
| 137 | |
| 138 | |
| 139 | print("Example 6") |