i have one file which is highcore.txt and it is empty.
i want if txt file is empty or if any number which is score is greater than any number in txt file , update the txt file , here is my code
JavaScript
x
11
11
1
def game():
2
return 115
3
4
score = game()
5
with open("highscore.txt") as f:
6
highScoreStr = f.read()
7
if int(highScoreStr)<score or highScoreStr=='':
8
with open("highscore.txt","w") as f:
9
f.write(str(score))
10
11
it is giving error
JavaScript
1
3
1
if int(highScoreStr)<score or highScoreStr=='':
2
ValueError: invalid literal for int() with base 10: ''
3
how to fix this error
Advertisement
Answer
I don’t think it is possible to pass an empty string through int()
Since you know it starts as an empty string you’re probably better off writing 2 if
statements:
JavaScript
1
7
1
if highScorStr == '':
2
with open("highscore.txt","w") as f:
3
f.write(str(score))
4
if int(highScoreStr)<score:
5
with open("highscore.txt","w") as f:
6
f.write(str(score))
7