如何解决:“ int”对象不可迭代

1 python int iterable object

我正在尝试运行这段代码,并且收到一条错误消息'int' object is not iterable for line 10。不知道我哪里出错了。

def inputVal(prompt,lower,upper):
    print(prompt)
    retVal = int(input())
    while retVal<lower or retVal>upper:
        print('Incorrect Value')
        retVal = int(input())
    return retVal

numComp = inputVal("Please enter number of competitors", 5, 20)

for comp in numComp:
    total=0
    for i in range(5):
        judgescore = inputVal('Please input judges score', 0, 10)
        total = total + judgescore
    print("Total judge score for competitor ", comp+1, "is: ", total)
    print("Average judge score for competitor ", comp+1, "is: ", total/5)
Run Code Online (Sandbox Code Playgroud)

rda*_*das 5

它来自以下行:

for comp in numComp:
Run Code Online (Sandbox Code Playgroud)

因为numCompint来自用户的。并且ints不能被迭代(对一个数字进行迭代没有任何意义)

也许您的意思是:

for comp in range(numComp):
Run Code Online (Sandbox Code Playgroud)