I have a list of highscores (saved in a text file as the score, space, name (no commas)). However, when I go to retrieve the data, it appears only the first line is read.
for line in previous_scores:
data = line.split()
current_data = data[0]
if int(current_data) > highscore:
highscore = current_data
highscore_names = [data[1]]
elif int(current_data) == highscore:
highscore_names.append(data[1])
For example, with the data below:
2 James
3 Anna
5 Emily
It would return the highscore as James, with his score being 2.
If my code is wrong could you please tell me what is wrong, and if your really awesome fix it. I don't mind if there is a better way to do this.
Edit:
The file is opened with:
previous_scores = open("Scores.txt", "a+")
Edit 2:
I added some extra lines on the end, and updated the code for testing to the following:
for line in previous_scores:
data = line.split()
current_data = data[0]
print "1 " + data[0]
print "2 " + current_data
if int(current_data) > highscore:
print "3 " + current_data
highscore = current_data
highscore_names = [data[1]]
elif int(current_data) == highscore:
print "4 " + current_data
highscore_names.append(data[1])
elif int(current_data) < highscore:
print "5 " + current_data
The first score always returns 1, 2 and 3, and then all other scores return 1, 2 and 5.