Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Class Excercise
- ##1
- class Student():
- def __init__(self, name, age, list_of_marks):
- self.name = name
- self.age = age
- # list_of_subjects = ["english", "science", "math"] ## not really required because it is redundant anyways but you can use it because it is in the question.
- self.marks = {
- "english": list_of_marks[0],
- "science": list_of_marks[1],
- "maths": list_of_marks[2],
- }
- b = Student("Bruce", 24, [92, 95, 99])
- a = Student("Alfred", 42, [29, 59, 88])
- # print(b.marks["maths"])
- # print(a.marks["maths"])
- ##2
- class Student():
- def __init__(self, name, age, list_of_marks):
- self.name = name
- self.age = age
- self.marks = {
- "english": list_of_marks[0],
- "science": list_of_marks[1],
- "maths": list_of_marks[2],
- }
- def get_all_subjects(self):
- return list(self.marks.keys()) # because all the keys in the dictionary are basically subjects
- def check_subject(self, subject):
- return subject in self.marks.keys()
- ### statement above is as good as statements below
- # if subject in self.marks.keys:
- # return True
- # else:
- # return False
- def getAverage(self):
- return sum(self.marks.values())/len(self.marks.values())
- b = Student("Bruce", 24, [92, 95, 99])
- a = Student("Alfred", 42, [29, 59, 88])
- print(b.getAverage())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement