Hacker Rank 30 day code Challenge (Inheritance and constructor in python)
Problem:
The parent class is Person. Make a subclass Student. The Student has attributes first name, last name, Id, Scores.
Input: First Name, Last Name, ID, Number of scores (e.g 2 or 3) , Scores
Take the average of scores and print.
(Starting Person class code and Ending code is already provided in hacker rank editor)
class Person:
def __init__(self, firstName, lastName, idNumber):
self.firstName = firstName
self.lastName = lastName
self.idNumber = idNumber
def printPerson(self):
print("Name:", self.lastName + ",", self.firstName)
print("ID:", self.idNumber)
class Student(Person):
def __init__(self, firstName, lastName, idNumber,scores):
self.firstName = firstName
self.lastName = lastName
self.idNumber = idNumber
self.scores = scores
def calculate(self):
sumscore=0
for i in range(numScores):
sumscore = sumscore +int(scores[i])
avg = sumscore / numScores
if avg >=90.0 and avg<=100.0:
return 'O'
elif avg >=80.0 and avg<=90.0:
return 'E'
elif avg>=70.0 and avg<=80.0:
return 'A'
elif avg>=55.0 and avg<=70.0:
return 'P'
elif avg>=40.0 and avg<=55.0:
return 'D'
else:
return'T'
line = input().split()
firstName = line[0]
lastName = line[1]
idNum = line[2]
numScores = int(input()) # not needed for Python
scores = list( map(int, input().split()) )
s = Student(firstName, lastName, idNum, scores)
s.printPerson()
print("Grade:", s.calculate())