TypeError: 'int' object is not iterable Python?

TypeError: 'int' object is not iterable Python?

我用这个函数查号:

def numValid(code):
    LOOKUP = (0, 2, 4, 6, 8, 1, 3, 5, 7, 9)
    code = reduce(str.__add__, filter(str.isdigit, code))
    evens = sum(int(i) for i in code[-1::-2])
    odds = sum(LOOKUP[int(i)] for i in code[-2::-2])
    return ((evens + odds) % 10 == 0)

我使用 numValid(242344) 调用它,其中 242344dtype: int64

为什么会出现错误:

TypeError: 'int' object is not iterable on the line:

filter(str.isdigit, code))

整数不是序列 - 它们只是原子的东西,所以你不能迭代它们。看起来您正在尝试将字符串函数应用于每个数字 - 您需要将 code 转换为字符串(即 str(code))。