Code with the error

laps = 3
for lap in laps:
    print("Lap", lap)

Error message (Python 3.12, last line)

TypeError: 'int' object is not iterable

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: Loops

Frequently 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).