'int' object is not iterable in strings

Viewed 94

So I was trying to get a list of the numbers 1,2,3... to print out individually on the console but it would always say int object is not iterable. My code:

def run_seed_code(told_seed):
    for letter in told_seed:
        print(letter)

run_seed_code(1234321)
1 Answers

Try converting the number into a string.

for char in "Hello World":
    print(char)

This prints each letter on a new line because it iterates through the string.

An integer is not iterable, you can't loop through it.

Either adding a string caster in the function or just making the argument a string will work fine.

def run_seed_code(told_seed):
    for letter in str(told_seed): # Convert to a string
        print(letter)

run_seed_code("1234321") # Or pass a string into the argument instead.
Related