无法迭代对象类型" int"错误而不迭代整数

时间:2016-10-14 11:21:27

标签: python-2.7 function int iteration

ans = raw_input("Please set the starting juvenile population (or type same to leave it unchanged):")
cont, num = setGen0Check(ans, 0)
if cont == 1:
    if num == 1:
        juvenilePop = round(float(ans), 3)
else:
    invalidInput()
    y = setGen0(x, y, z)

def setGen0Check(ans, bounds):
    cont = 1
    cont2 = 1
    num = (numOrStrCheck(46, 57, ans))
    if ans.lower() != "same" and num == 0:
        invalidInput()
        count = 0
    elif len(ans) == 0:
        invalidInput()
        cont = 0
    elif num == 1:
        if float(ans) < 0:
            invalidInput()
            cont = 0
        if bounds == 1:
            cont2 = setGen0bounds(0, 1, ans)
        if bounds == 2:
            cont2 = setGen0bounds(15, 25, ans)
    if cont == 1 and cont2 == 1:
        return 1
    else:
        return 0
    return num

def numOrStrCheck(lowerBnd, higherBnd, ans):
    for i in ans:
        b = ord(i)
        if b > higherBnd or b < lowerBnd:
            return 0
    return 1

每当我给它一个输入时,我都会收到错误:

  

TypeError: 'int' object is not iterable

带有线路参考:

  

cont, num = setGen0Check(ans, 0)

它一直工作,直到我决定允许字符串输入(要求我将是否是一个数字传递给输入的同一个函数)。

2 个答案:

答案 0 :(得分:0)

这与raw_input无关。

函数setGen0Check返回单个整数值。执行语句cont, num = setGen0Check(ans, 0)时,解释器会尝试迭代返回的值,因为它需要2个值来分配给contnum。然后,引发异常,因为它无法迭代int。

请在提出类似问题时发布完整的追溯,这将非常有用。

答案 1 :(得分:0)

更改代码
for i in ans:

for i in range(ans):

整数不是可迭代的对象。