1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
#!/usr/bin/python3
from MITPerson import *
class Grades(object):
""" A mapping from students to a list of grades """
def __init__(self):
""" Create empty grade book """
self.students = [] # List of student objects
self.grades = {} # maps idNum -> list of grades
self.isSorted = True # true if self.students is sorted
def addStudent(self, student):
"""
Assumes: student is of type Student
Add student to the grade book
"""
if student in self.students:
raise ValueError('Duplicated student')
self.students.append(student)
self.grades[student.getIdNum()] = [] # dict is a ID:List pair
self.isSorted = False
def addGrade(self, student, grade):
"""
Assumes: grade is a float
Add grade to the list of grades for student
"""
try:
self.grades[student.getIdNum()].append(grade)
except KeyError:
raise ValueError('Student not in grade book')
def getGrades(self, student):
""" Return a list of grades for student """
try: # Return a COPY of student's grades
return self.grades[student.getIdNum()][:]
except KeyError:
raise ValueError('Student not in grade book')
def allStudents(self):
""" Return a list of the students in the grade book """
if not self.isSorted:
self.students.sort()
self.isSorted = True
return self.students[:] # Return a COPY of students, so we avoid any
# externalmodification to the original list
### TESTS
def gradeReport(course):
""" Assumes: course is of type grades """
report = []
for s in course.allStudents():
tot = 0.0
numGrades = 0
for g in course.getGrades(s):
tot += g
numGrades += 1
try:
mean = tot/numGrades
report.append(str(s) + '\'s mean grade is ' + str(mean))
except ZeroDivisionError:
report.append(str(s) + ' has no grades')
return ' \n'.join(report)
six00 = Grades()
ug3 = UG('Drew Houston', 2017)
ug1 = UG('Matt Damon', 2018)
ug2 = UG('Ben Affleck', 2019)
ug4 = UG('Mark Zuckerberg', 2017)
g1 = Grad('Bill Gates')
g2 = Grad('Steve Wozniak')
six00.addStudent(g1)
six00.addStudent(ug2)
six00.addStudent(ug1)
six00.addStudent(g2)
six00.addStudent(ug4)
six00.addStudent(ug3)
six00.addGrade(g1, 90)
six00.addGrade(g2, 45)
six00.addGrade(ug1, 80)
six00.addGrade(ug2, 75)
|