Advertisement
Arcot

ch11 ex2

Dec 26th, 2021
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. ##2
  2. class Student():
  3.  
  4.     def __init__(self, name, age, list_of_marks):
  5.         self.name = name
  6.         self.age = age
  7.         self.marks = {
  8.             "english": list_of_marks[0],
  9.             "science": list_of_marks[1],
  10.             "maths": list_of_marks[2],
  11.         }
  12.    
  13.     def get_all_subjects(self):
  14.         return list(self.marks.keys()) # because all the keys in the dictionary are basically subjects
  15.    
  16.     def check_subject(self, subject):
  17.         return subject in self.marks.keys()
  18.  
  19.         ### statement above is as good as statements below
  20.         # if subject in self.marks.keys:
  21.         #     return True
  22.         # else:
  23.         #     return False
  24.  
  25.     def getAverage(self):
  26.         return sum(self.marks.values())/len(self.marks.values())
  27.  
  28.  
  29. b = Student("Bruce", 24, [92, 95, 99])
  30. a = Student("Alfred", 42, [29, 59, 88])
  31.  
  32. print(b.getAverage())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement