Write Python. Understand every error.

Open the editor

Error guide · TypeError

TypeError: 'int' object is not iterable

A loop or function expected something with several items, but it was given a single number.

Example

for i in 5:
    print(i)

What Python says

TypeError: 'int' object is not iterable

As printed by Python 3.12. Other versions may word it slightly differently.

Try this code in Py-CRAFT

Why it happens

A for loop visits the items of a list, a string, a range or another collection. The number 5 is one value, so there is nothing to visit. range(5) produces the numbers 0 to 4. Functions such as sum(5), list(5) and max(5) fail for the same reason.

How to fix it

for i in range(5):
    print(i)

Output: 0 ⏎ 1 ⏎ 2 ⏎ 3 ⏎ 4

Tips

Learn the ideas behind it

Other common errors

Share this page: