这是我试图运行的代码......
scoreCount = int(input("How many scores do you want to record?"))
recordedValues = 0
averageScore = totalScore/scoreCount
highestScore = 0
totalScore = 0
while recordedValues <= scoreCount:
score = int(input("\n\nEnter Score,:")
if type(score)== int:
totalScore == totalScore + score
recordedValues == recordedValues + 1
if score >= highestScore:
highestScore = score
else:
print("\n\nThe scores are not integer values")
quit()
如何让while循环结束并显示平均分数/最高分/记录值?
你在while循环中放置==而不是=:
if type(score)== int:
totalScore == totalScore + score
recordedValues == recordedValues + 1
所以'recordedValues'和'totalScore'没有改变。
编辑:'khelwood'已在评论中提到它。
你的代码存在很多问题:你甚至在averageScore
定义之前尝试将totalScore/scoreCount
分配给totalScore
,你有时使用==
相等检查器作为赋值运算符,你检查score
是否在int
中,即使它已被转换,并且你的while循环中有条件问题。这是你可以做的:
用异常处理替换重复类型测试并删除非法变量赋值:
try:
scoreCount = int(input("How many scores do you want to record?"))
except ValueError:
print("\n\nYou need to enter an integer...")
quit()
recordedValues = 0
highestScore = 0
totalScore = 0
将>=
更改为>
以获得最高分,修复变量赋值,并使用异常处理替换不需要的类型检查。
while recordedValues <= scoreCount:
try:
score = int(input("\n\nEnter Score: "))
except ValueError:
print('Scores must be numbers.')
quit()
totalScore += score
recordedValues += 1
if score > highestScore:
highestScore = score
print("\n\nThe amount of values recorded:", recordedValues)
print("\n\nThe average score:", totalScore / scoreCount)
print("\n\nThe highest score:", highestScore)