Home > Software engineering >  TypeError: 'int' object is not iterable Python?
TypeError: 'int' object is not iterable Python?

Time:05-24

I use this function to check number:

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)

I call it using numValid(242344) where 242344 is dtype: int64.

Why I get error:

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

filter(str.isdigit, code))

CodePudding user response:

Integers aren't sequences - they're just atomic things, so you can't iterate over them. It looks like you're trying to apply a string function to each digit - you'll need to convert code to a string (i.e. str(code)).

CodePudding user response:

Can u provide more details as what are you trying to achieve?

  • Related