Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Student(object):
- def __init__(self, course):
- """Initialises object 'Student' with a course name.
- """
- self.course = course
- self.subjectList = []
- def addSubject(self, *args):
- """Defines (a) new object(s) from class 'Student'.
- Adds that object to self.subjectList.
- """
- for newSubjectName in args:
- self.subjectList.append(self.Subject(newSubjectName))
- def printStats(self):
- """Prints the names of the subjects in subjectList."""
- for i in range(len(self.subjectList)):
- print("Subject " + str(i+1) + ": " + self.subjectList[i].subjectName)
- def avgMark(self):
- """Loops through all subjects in subjectList.
- Appends the sum of the subject's marks to total.
- Appends the amount of marks in the subject to count.
- Returns the average.
- """
- total = 0
- count = 0
- for subject in self.subjectList:
- total += sum(subject.marks)
- count += len(subject.marks)
- return total / count
- class Subject(object):
- def __init__(self, subjectName):
- self.subjectName = subjectName
- self.marks = []
- def assignMarks(self, *args):
- """Takes marks from *args. Appends them to self.marks.
- Marks must be between 1 and 10. If they're not, an exception will be raised.
- """
- for mark in args:
- if mark >= 1 and mark <= 10:
- self.marks.append(mark)
- else:
- raise Exception("Incorrect value for mark")
- def main():
- s = Student("Software engineering")
- s.addSubject("Programming", "Analytics", "Maths", "Computer Organisation")
- s.printStats()
- s.subjectList[0].assignMarks(7, 3.8, 10)
- s.subjectList[1].assignMarks(6.7, 5, 6)
- s.subjectList[2].assignMarks(7.3, 1)
- s.subjectList[3].assignMarks(8.5)
- print(s.avgMark())
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment