I have a text file with students scores from taking a test and I need to find each students highest score and then print each students highest score, in the order of highest to lowest.
My code is down below however I keep getting tuple index out of range.
classChoice = str(input('What class scores would you like to view; rat, wolf or polar bear '))
fileName = 'results/' + classChoice + ".txt"
# init a list where you store the results
results = []
# open the file with results in a "read" mode
with open(fileName, "r") as fileinput:
# for each line in file with results, do following
for line in fileinput:
# remove whitespaces at the end of the line and split the line by ":"
items = line.strip().split(":")
# store the result as a list of tuples
results.append(tuple(items))
# first it sorts all the tuples in `results` tuple by the second item (score)
# for each result record in sorted results list do the following
for result_item in sorted(results, key=lambda x: x[1], reverse=True):
# print the result in the format of the scores in your file
print ("{}:{}".format(result_item[0], result_item[1]))