+ 2

Is it possible to seek more than 1 text in a text file using the seek method?

Hello, what am try to do is create this high score system for my game. Initially, all it does is to store an integer (converted to string first) in a text file. If the current score is greater than the high score that it saved from previous game plays, it will clear that score and write the current score as the high score. But the issue is, it's only comparing the first integers in the scores, for eg. say that the current high score is now 70 and i play the game and got a score of 110. It will not save 110 because its comparing the 1 to the 7 only and not the entire integer. How can I fix this? This is my code: stop = 0 def HighScore(): global stop if stop<1: with open('HighScore.txt','r+') as hs: for score in hs: if for_cfile > score: hs.seek(0) hs.truncate() hs.write(for_cfile) print('New HighScore!\n',for_cfile,'\nCongratulations!') print("\nGame Over") stop += 1

25th Jan 2019, 11:09 AM
Leo Hunter
Leo Hunter - avatar
2 Respostas
+ 7
use int(score), which convert data type to integer.
25th Jan 2019, 11:52 AM
Hubert Dudek
Hubert Dudek - avatar
+ 1
Thank you! It worked
25th Jan 2019, 12:42 PM
Leo Hunter
Leo Hunter - avatar