MCPcopy Index your code
hub / github.com/bslatkin/effectivepython / WeightedGradebook

Class WeightedGradebook

example_code/item_029.py:111–136  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

109
110print("Example 5")
111class 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
139print("Example 6")

Callers 1

item_029.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected