[英]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.如果 highscore.txt 只有 0,它会工作,但如果它是任何数字,我就会收到错误。 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.
如果我在 if 语句之前检查 highscore 的值(使用 pycharm 调试),它会给我一个表示最高分的字符串值,这正是我所期望的。 Though, whent trying to print it, says it is n.netype.
虽然,当试图打印它时,说它是 n.netype。 Why is it giving me this error?
为什么会给我这个错误?
For some reason the script created spaces before the number in the file.由于某种原因,脚本在文件中的数字之前创建了空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.