Write Python. Understand every error.

Open the editor

Error guide · IndentationError

IndentationError: expected an indented block

The spaces at the start of a line are wrong for the structure of the code.

Example

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

What Python says

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

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

Try this code in Py-CRAFT

Why it happens

Python uses indentation (spaces at the start of a line) to know which lines belong inside a loop, if or function. After a line ending with a colon, the next line must be indented, and lines in the same block must line up exactly.

How to fix it

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

Output: 0 ⏎ 1 ⏎ 2

Tips

Learn the ideas behind it

Other common errors

Share this page: