Python line.split only acts on one line -
i have list of highscores (saved in text file score, space, name (no commas)). however, when go retrieve data, appears first line 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, data below:
2 james 3 anna 5 emily
it return highscore james, score being 2.
if code wrong please tell me wrong, , if awesome fix it. don't mind if there better way this.
edit:
file opened with:
previous_scores = open("scores.txt", "a+")
edit 2:
added lines on end, , updated code testing 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 returns 1, 2 , 3, , other scores return 1, 2 , 5.
if int(current_data) > highscore: highscore = current_data
here convert current highscore number, store unconverted string. next loop comparing next highscore number saved string.
i don't know comparing number > string in python, it's legal syntax , returns false, never save higher highscore after first one.
edit: know, , it's interesting irrelevant answer. https://stackoverflow.com/a/3270689/478656 , https://stackoverflow.com/a/2384139/478656
Comments
Post a Comment