Finding the Hishest Score. ValueError: invalid literal for int() with base 10: ''
Finding the Hishest Score. ValueError: invalid literal for int() with base 10: ''
我正在创建一个测验,其中每个用户的分数都保存到一个外部文本文件中。但是,每当我在数学简单测验中输出最高分的报告时,它都会说:ValueError: invalid literal for int() with base 10: ''
这似乎是问题所在:
if highestScore <= int(line.strip()):
with open("mathsEasy.txt") as mathsEasyFile:
highestScore = 0
for line in mathsEasyFile:
if highestScore <= int(line.strip()):
highestScore = int(line.strip())
mathsEasyFile.close()
print "The highest score is", highestScore
基本上,每次用户做数学简单测验时,它都会将他们的分数保存到名为 mathsEasy.txt
的文本文件中
文本文件如下所示: the username : score
例如 Kat15 : 4
我只需要输出最高分,而不是用户名。
一些事情...
- 它可能只是在 Whosebug 中进行格式化,但要确保你的缩进是正确的;在
mathsEasyFile:
之后,您可能想要缩进最后的打印语句以外的所有内容。
- 我认为您会想要添加类似
lines = mathsEasyFile.readlines()
的内容并使用类似 for line in lines:
的内容进行迭代。我不相信您当前的设置没有读取任何文件内容。 不正确。请参阅下面的评论。
mathsEasyFile.close()
已通过使用 with
语句 为您完成
澄清编辑
# something like this ought to work
with open("mathsEasy.txt") as mathsEasyFile:
highestScore = 0
for line in mathsEasyFile:
if highestScore <= int(line.strip()):
highestScore = int(line.strip())
print "The highest score is", highestScore
现在您已经添加了文件工作原理的示例:
with open("mathsEasy.txt") as mathsEasyFile:
highestScore = max(int(line.split(' : ')[1]) for line in mathsEasyFile if len(line.strip() != 0)
print("The highest score is %d" % highestScore)
分解帮助你更好地理解它:
highestScore = 0 # in my previous code, I use max() instead
with open("mathsEasy.txt") as mathsEasyFile: # open the file
for line in mathsEasyFile: # for each line in the file,
if len(line.strip()) == 0: # if the current line is empty,
continue # ignore it and keep going to the next line
_,score = line.split(' : ') # split the line into its components
if int(score) > highestScore: # if this score is better,
highestScore = int(score) # replace the best score
print("The highest score is %d" % highestScore)
我正在创建一个测验,其中每个用户的分数都保存到一个外部文本文件中。但是,每当我在数学简单测验中输出最高分的报告时,它都会说:ValueError: invalid literal for int() with base 10: ''
这似乎是问题所在:
if highestScore <= int(line.strip()):
with open("mathsEasy.txt") as mathsEasyFile:
highestScore = 0
for line in mathsEasyFile:
if highestScore <= int(line.strip()):
highestScore = int(line.strip())
mathsEasyFile.close()
print "The highest score is", highestScore
基本上,每次用户做数学简单测验时,它都会将他们的分数保存到名为 mathsEasy.txt
的文本文件中
文本文件如下所示: the username : score
例如 Kat15 : 4
我只需要输出最高分,而不是用户名。
一些事情...
- 它可能只是在 Whosebug 中进行格式化,但要确保你的缩进是正确的;在
mathsEasyFile:
之后,您可能想要缩进最后的打印语句以外的所有内容。 - 我认为您会想要添加类似
lines = mathsEasyFile.readlines()
的内容并使用类似for line in lines:
的内容进行迭代。我不相信您当前的设置没有读取任何文件内容。 不正确。请参阅下面的评论。 mathsEasyFile.close()
已通过使用with
语句 为您完成
澄清编辑
# something like this ought to work
with open("mathsEasy.txt") as mathsEasyFile:
highestScore = 0
for line in mathsEasyFile:
if highestScore <= int(line.strip()):
highestScore = int(line.strip())
print "The highest score is", highestScore
现在您已经添加了文件工作原理的示例:
with open("mathsEasy.txt") as mathsEasyFile:
highestScore = max(int(line.split(' : ')[1]) for line in mathsEasyFile if len(line.strip() != 0)
print("The highest score is %d" % highestScore)
分解帮助你更好地理解它:
highestScore = 0 # in my previous code, I use max() instead
with open("mathsEasy.txt") as mathsEasyFile: # open the file
for line in mathsEasyFile: # for each line in the file,
if len(line.strip()) == 0: # if the current line is empty,
continue # ignore it and keep going to the next line
_,score = line.split(' : ') # split the line into its components
if int(score) > highestScore: # if this score is better,
highestScore = int(score) # replace the best score
print("The highest score is %d" % highestScore)