为什么while循环没有结束?

2024-09-27 09:30:59 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我试图运行的代码。。。你知道吗

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循环并显示平均分数/最高分数/记录的值?你知道吗

编辑:谢谢你的帮助,我已经解决了这个问题


Tags: 代码inputifdo分数manyhowint
2条回答

代码中有很多问题:在定义totalScore之前尝试将averageScore赋值给totalScore/scoreCount,有时使用==等式检查器作为赋值运算符,检查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)

在while循环中放入==而不是=:

if type(score)== int:
    totalScore == totalScore + score
    recordedValues == recordedValues + 1

所以“recordedValues”和“totalScore”没有改变。你知道吗

编辑:“khelwood”已经在评论中提到了它。你知道吗

相关问题 更多 >

    热门问题