I am writing some code to take high score data from an external file, take the top 5, and display them. I am nearly there, but my code only takes the first digit of the high score, so scores like 12 are seen to be smaller than scores like 8. Is there a way to take the whole number into consideration when sorting?
My external .txt file looks like this:
('Louis', 18)
('Louis', 4)
('Louis', 2)
('Louis', 4)
('Troy', 0)
('Andre', 6)
('Louis', 1)
('Louis', 17)
('Louis', 3)
('Louis', 9)
and the output looks like this:
('Louis', 9)
('Andre', 6)
('Louis', 6)
('Louis', 4)
('Louis', 4)
This is my code:
with open('highscore.txt', 'r') as score_file:
scores = [line.split(' ') for line in score_file]
scores.sort(key=lambda x: x[1], reverse=True)
print('The highscores are...\n')
for i in range(0, 5):
print(' '.join(scores[i]))