简体   繁体   中英

When converting string to integer from txt file, I get ValueError: invalid literal for int() with base 10:

I am trying to create an external highscore file.

highscoreFile = open("highscore.txt", "r+")
highscore = highscoreFile.read()

if current > int(highscore):
    highscoreFile.truncate(0)
    print(highscore)

It works if highscore.txt has only a 0, but if it is any number I get the error. If I check the value of highscore right before the if statement (using pycharm debug), it gives me a string value of what the high score is, which is what I would expect. Though, whent trying to print it, says it is n.netype. Why is it giving me this error?

For some reason the script created spaces before the number in the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM