더북(TheBook)

다음으로 Grade 인스턴스 집합을 포함하면서 한 과목을 표현하는 클래스를 작성할 수 있다.

class Subject:
    def __init__(self):
        self._grades = []

    def report_grade(self, score, weight):
        self._grades.append(Grade(score, weight))

    def average_grade(self):
        total, total_weight = 0, 0
        for grade in self._grades:
            total += grade.score * grade.weight
            total_weight += grade.weight
        return total / total_weight