forked from drycode/zelle-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpa.py
51 lines (40 loc) · 1.33 KB
/
gpa.py
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
#gpa.py
# Program to find student with highest GPA
class Student:
def __init__(self, name, hours, qpoints):
self.name = name
self.hours = float(hours)
self.qpoints = float(qpoints)
def getName(self):
return self.name
def getHours(self):
return self.hours
def getQPoints(self):
return self.qpoints
def gpa(self):
return self.qpoints / self.hours
def makeStudent(infoStr):
#inforStr is a tap-separated line: name hours getQPoints
#returns a corresponding Student object
name, hours, qpoints = infoStr.split("\t")
return Student(name, hours, qpoints)
def main():
#open the input file for reading
filename = input("Enter the name of the grade file: ")
infile = open(filename, 'r')
#set best to the record for the first student in the file
best = makeStudent(infile.readline())
#process subsequent lines of the file
for line in infile:
#turn the line into a student record
s = makeStudent(line)
#if this student is best so far, remember it.
if s.gpa() > best.gpa():
best = s
infile.close()
#print information about the best student
print("The best student is:", best.getName())
print("hours:", best.getHours())
print("GPA:", best.gpa())
if __name__ == '__main__':
main()