Home > other >  Python challenge TypeError: 'int' object is not iterable
Python challenge TypeError: 'int' object is not iterable

Time:10-25

This is one of the challenge question: Write a program, which will find all such numbers between 1000 and 3000 (both included) such that each digit of the number is an even number. The numbers obtained should be printed in a comma-separated sequence on a single line. Hints: In case of input data being supplied to the question, it should be assumed to be a console input. Here is what I'm trying to do: if there's an input, then check if all the digits are even numbers and display a message. If there's no input, then it will append the number from 1000 to 3000 that contains all even digits to a list. I heard nested loop is bad practice, so better solution is welcomed. Encountered Error: TypeError: 'int' object is not iterable.

num = input("Enter a number to check for even digit. Skip if you want to see 1000-3000: ")
if num:
    list = [int(x) for x in num]
    for i in list:
        if i % 2 != 0:
            x = False
            break
        else:
            x = True
    if x:
        print("It has all even digits!")
    else:
        print("It contains odd digits.")
else:
    t = True
    printl = []
    for i in range(1000,3001):
        l = [str(x) for x in i] # the error line
        for q in l:
            if int(q) % 2 != 0:
                t = False
                break
            else:
                printl.append(q)
    print(printl)

CodePudding user response:

You are trying to iterate over the integer i in the for loop below:

for i in range(1000,3001):
    l = [str(x) for x in i] # the error line

If you want to iterate over the string representation of that integer, use this instead:

for i in range(1000,3001):
    l = [x for x in str(i)] # the error line

CodePudding user response:

So your main issue (question) is about that integers are not iterable on this line:

for i in range(1000,3001):
    l = [str(x) for x in i] # the error line

so you are trying to iterate over i which is of type int which cannot be iterated, you need to convert it to a string first so this:

for i in range(1000,3001):
    l = [x for x in str(i)]

That however is quite redundant because you can immediately do this:

l = [int(x) for x in str(i)]

and avoid conversion to int later.

However your logic is flawed, in this loop it matters only if the last digit is even or not:

for i in list:
    if i % 2 != 0:
        x = False
        break
    else:
        x = True

Other flaw is in the second loop where you append all even numbers to that list so it will basically create a list of 0, 2, 4, 6, 8 because you append single digits. Over all your code can be reduced to this (at least):

# create a list to append to
all_even = []
# ask for user input
num = input('Enter a number to check for even digit. Skip if you want to see 1000-3000: ')
# loop over either user input if they did input anything or over the range
# if they didn't input anything
for i in (num and [num]) or range(1000, 3000   1):
    # check if all digits in the number are even and
    # if they are append to the list
    if all(int(x) % 2 == 0 for x in str(i)):
        all_even.append(i)

# now check if user did input anything and if they did return the boolean
# of the list `all_even` which if the number had all even digits
# will contain one item so the bool will return True and or returns first True
# value or last one if none are True. So in case user did not input anything go to the
# next logical operator `or` and if there is anything in `all_even` print it out
# lastly print False if there were no values found at all
print(num and bool(all_even) or all_even or False)

Sources:

  • Related