class Student:
def __init__(self,n,m):
self.name = n
self.major = m
self.courses = []
def add_course(self,c):
self.courses.append(c)
def __str__(self):
return self.name + ":" + self.major
class Course:
def __init__(self,n,t):
self.cno = n
self.title = t
self.roster = []
def add_student(self,s):
self.roster.append(s)
def __str__(self):
return self.cno + ":" + self.title
s1 = Student("John","CSC")
s2 = Student("Sally","CSC")
s3 = Student("Alice","MATH")
c1 = Course("1301","CS I")
c2 = Course("1302","CS II")
s1.add_course(c1)
s1.add_course(c2)
s2.add_course(c1)
c1.add_student(s1)
c1.add_student(s2)
c2.add_student(s1)
# print the roster for CSC 1301, the c1 object
for s in c1.roster:
print(s)
John:CSC Sally:CSC
students = [s1,s2,s3]
for s in students:
if c1 in s.courses:
print(s)
John:CSC Sally:CSC