Q: Print all the students with the top marks. If multiple students have the same mark, print both students.
Sample Output:
Best student(s): Micheal Murphy, Dan Smith
Best mark: 89
My code:
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])
My output that I am getting:
Best student(s): Michael Murphy
Best mark: 89
My problem:
How do I make it so that it prints multiple students that achieved the top mark? I want my output to be
Best student(s): Micheal Murphy, Dan Smith
Best mark: 89