← All Python errors

Python error explained

IndentationError: what it means and how to fix it

The spaces at the start of a line do not match what Python expects.

The message

IndentationError: expected an indented block after 'for' statement on line 2

What it means

Python uses indentation to know which lines belong inside an if, a loop or a function. After a line ending in a colon, the next line must be indented. Lines in the same block must line up exactly.

Common causes

  • Forgetting to indent the line after a colon
  • Mixing tabs and spaces
  • One line in a block indented by 3 spaces and the rest by 4
  • Indenting a line that should not be inside anything

Example that raises it

names = ["Ada", "Grace", "Linus"]
for name in names:
print("Hello", name)

The fix

names = ["Ada", "Grace", "Linus"]
for name in names:
    print("Hello", name)

Tip: Use four spaces per level and let your editor insert them. Pythonaut's editor turns the Tab key into four spaces for you.

Fix the broken version yourself in the editor on this page. It runs real Python in your browser.

Start the free lessons More Python errors