Python Day 12: Inheritance




You are given two classes, Person and Student, where Person is the base class and Student is the derived class. Completed code for Person and a declaration for Student are provided for you in the editor. Observe that Student inherits all the properties of Person.



  1. class Person:
  2. def __init__(self, firstName, lastName, idNumber):
  3. self.firstName = firstName
  4. self.lastName = lastName
  5. self.idNumber = idNumber
  6. def printPerson(self):
  7. print("Name:", self.lastName + ",", self.firstName)
  8. print("ID:", self.idNumber)
  9. class Student(Person):
  10. def __init__(self, firstName, lastName, idNumber, testScores):
  11. super().__init__(firstName, lastName, idNumber)
  12. self.testScores = testScores
  13. def calculate(self):
  14. total = 0
  15. for testScore in self.testScores:
  16. total += testScore
  17. avg = total / len(self.testScores)
  18. if 90 <= avg <= 100:
  19. return 'O'
  20. if 80 <= avg < 90:
  21. return 'E'
  22. if 70 <= avg < 80:
  23. return 'A'
  24. if 55 <= avg < 70:
  25. return 'P'
  26. if 40 <= avg < 55:
  27. return 'D'
  28. return 'T'
  29. line = input().split()
  30. firstName = line[0]
  31. lastName = line[1]
  32. idNum = line[2]
  33. numScores = int(input()) # not needed for Python
  34. scores = list( map(int, input().split()) )
  35. s = Student(firstName, lastName, idNum, scores)
  36. s.printPerson()
  37. print("Grade:", s.calculate())
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution






codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube