All Python errors
TypeError: 'int' object is not iterable
This message means Python tried to go through the items of a whole number, and a number has none. To repeat something n times write for i in range(n); to total several numbers, put them in a list first.
Code with the error
laps = 3
for lap in laps:
print("Lap", lap)
Error message (Python 3.12, last line)
Corrected code
laps = 3
for lap in range(laps):
print("Lap", lap + 1)
Output
Lap 1
Lap 2
Lap 3
Common causes
- for x in n instead of for x in range(n).
- sum(), max() or list() receives a single number.
- A function returned a number where a list was expected.
- Unpacking a number into two names: a, b = 5.
Practise in the course
Lesson: LoopsFrequently asked questions
What does iterable mean?
A value you can take items from one by one: a list, a string, a tuple, a dictionary or a range.
How do I loop over the digits of a number?
Turn it into text first: for digit in str(n).