I am trying to create leaderboard for my game. After the game ends, it asks for name and calculates score. However, when I write the Name and Score to the leaderboard.txt file and i try to import them back again, I cannot convert the number back to int from string. I need to convert it back to int so that i can sort the leaderboard by the most score achieved, or do I?
score = 5
name = "Adam"
file = open("leaderboard.txt", "a+")
file.write(name+" ")
file.write(str(score)+"\n")
file.close()
file = open("leaderboard.txt", "r")
list_of_lists = []
for line in file:
list_line = line.split()
list_of_lists.append(list_line)
for x in range(0,len(list_of_lists)):
if list_of_lists[x][1].isdigit == True:
x = int(x)
print(list_of_lists)
This does not work, the number stays string however no errors appear. I really dont know what I am missing. Is this even a right way of writing name and score to .txt file? Is there a better way to think about this? I am really desperate for help.