-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass Inheritance
More file actions
51 lines (47 loc) · 1.72 KB
/
Copy pathClass Inheritance
File metadata and controls
51 lines (47 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
### Link to the problem description: https://www.hackerrank.com/challenges/30-inheritance/problem
### Header Locked stub code section (by HackerRank)
## Person class is defined, the __init__ and print methods are defined, which later are used by the new student class.
# Inputs are first, last name and ID
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)
###
### My code section
class Student(Person):
## Init method called on the inherited person class, defines the student. Scores are part of the input and stored
def __init__(self, firstName, lastName, idNumber, scores):
Person.__init__(self, firstName, lastName, idNumber)
self.scores = scores
# An if chain represents a switch used to define the student's grade based on test scores
def calculate(self):
mean = sum(scores)/len(scores)
grade = None
if mean < 40:
grade = "T"
elif mean < 55:
grade = "D"
elif mean < 70:
grade = "P"
elif mean < 80:
grade = "A"
elif mean < 90:
grade = "E"
else:
grade = "O"
return grade
### Footer Locked stub code section (by HackerRank)
## Stores input and calls upon the newly defined student's class methods
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())