Skip to main content
 首页 » 编程设计

python之我该如何修复 : 'int' object is not iterable

2025年05月04日22lovecherry

我正在尝试运行这段代码,但我收到一条错误消息 '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) 

请您参考如下方法:

它来自这一行:

for comp in numComp: 

因为 numComp 是一个来自用户的 int。并且 int 无法迭代(迭代数字没有任何意义)

也许你的意思是:

for comp in range(numComp):