为什么while循环结束?

问题描述 投票:0回答:2

这是我试图运行的代码......

scoreCount = int(input("How many scores do you want to record?"))
recordedValues = 0
averageScore = totalScore/scoreCount
highestScore = 0
totalScore = 0

This is where I believe the code stops working...

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循环结束并显示平均分数/最高分/记录值?

Edit: Thanks for the help, I have solved the problem.

python while-loop
2个回答
2
投票

你在while循环中放置==而不是=:

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

所以'recordedValues'和'totalScore'没有改变。

编辑:'khelwood'已在评论中提到它。


1
投票

你的代码存在很多问题:你甚至在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)
© www.soinside.com 2019 - 2024. All rights reserved.