I'm making a highscore list, and the order of it should determined by number of points, the second element of the lists in list. This is my code:
from typing import List, Tuple
name1 = 'John'
name2 = 'Ron'
name3 = 'Jessie'
points1 = 2
points2 = 3
points3 = 1
highscore: List[Tuple[str, int]] = []
highscore.append((name1, points1))
highscore.append((name2, points2))
highscore.append((name3, points3))
print(highscore)
sorted_by_second = sorted(highscore, key=lambda X: X[1])
highscore_list= str(sorted_by_second)
export list to file
with open('highscore.txt', 'w') as f:
for item in highscore_list:
f.write("%s\n" % item)
Then it looks like this in the file:
[
(
J
e
s
s
i
e
,
1
)
,
But I want it to look like this in the file:
Jessie 1
John 2
How do I achieve this?