【发布时间】:2021-05-08 20:21:41
【问题描述】:
问:打印所有得分最高的学生。如果多个学生的分数相同,请打印两个学生。
样本输出:
Best student(s): Micheal Murphy, Dan Smith
Best mark: 89
我的代码:
import sys
def procfile(filename):
try:
with open(filename, 'r') as f:
bestMark = -1
for line in f:
mark, name = line.strip().split(maxsplit=1)
mark = int(mark)
if mark > bestMark:
bestMark, bestStudent = mark, name
print(f'Best student(s): {bestStudent}')
print(f'Best mark: {bestMark}')
except FileNotFoundError:
print(f'The file {filename} cannot be opened')
procfile(sys.argv[1])
我得到的输出:
Best student(s): Michael Murphy
Best mark: 89
我的问题:
如何让它打印出多个获得最高分的学生? 我希望我的输出是
Best student(s): Micheal Murphy, Dan Smith
Best mark: 89
【问题讨论】:
-
至少你在问题中包含minimal reproducible example...
标签: python