class Student:
#构造函数
def __init__(self, name, hours, qpoints):
#Student类的属性
self.name = name
self.hours = hours
self.qpoints = qpoints
#Student类的行为
def getName(self):
return self.name
def getHours(self):
return self.hours
def getQpoints(self):
return self.qpoints
def getGpa(self):
return self.qpoints / self.hours
def makeStudent(intoStr):
name, hours, qpoints = intoStr.split()
return Student(name, int(hours), int(qpoints))
def main():
filename = input('please input a filename').strip()
#用 utf-8 编码打开 (实际文件的编码方式)
infile = open(filename, 'r', encoding='utf-8')
best = makeStudent(infile.readline())
for line in infile:
s = makeStudent(line)
if best.getGpa() < s.getGpa():
best = s
infile.close()
print('the best student is:', str(best.getName()))
print('hours:', best.getHours())
print('qpoints:', best.getQpoints())
print('gpa:', best.getGpa())
if __name__ == '__main__':
main()
源文件:
结果: