Editor

IndentationError: expected an indented block

What it means

A line that ends with a colon, such as an if, for, while or def line, must be followed by at least one indented line. Python found the next line at the same level as the colon line, so the block is empty. The message names the statement and the line it started on.

1. Forgetting to indent the body of an `if`

This raises the error

Broken
age = 20
if age >= 18:
print("You can vote")
Output
Traceback (most recent call last):
  File "<exec>", line 3
    print("You can vote")
    ^^^^^
IndentationError: expected an indented block after 'if' statement on line 2

The fix

Fixed
age = 20
if age >= 18:
print("You can vote") # 4 spaces put it inside the if
Output
You can vote

Python uses indentation, not brackets, to know which lines belong to the if. Indent them by 4 spaces, the standard.

Try fixing it yourself in the editor

2. A loop body that is not indented

This raises the error

Broken
total = 0
for n in [4, 8, 15]:
total += n
print("Total:", total)
Output
Traceback (most recent call last):
  File "<exec>", line 3
    total += n
    ^^^^^
IndentationError: expected an indented block after 'for' statement on line 2

The fix

Fixed
total = 0
for n in [4, 8, 15]:
total += n # indented: repeats for every n
print("Total:", total) # not indented: runs once, after the loop
Output
Total: 27

Indent the lines that should repeat. The print stays at the left edge, so it runs once after the loop finishes.

Try fixing it yourself in the editor

3. A function with nothing in it yet

This raises the error

Broken
def greet(name):
# I will write this later
print("Program started")
Output
Traceback (most recent call last):
  File "<exec>", line 4
    print("Program started")
    ^^^^^
IndentationError: expected an indented block after function definition on line 1

The fix

Fixed
def greet(name):
pass # a placeholder until you write the body
print("Program started")
Output
Program started

A comment does not count as a statement, so the function is still empty. pass is a statement that does nothing, which fills the block until you write the real code.

Try fixing it yourself in the editor

When you see this error, check

  • Look at the line Python names in the message: it ends with a colon and needs an indented line after it.
  • Indent every line of the block by the same amount, usually 4 spaces.
  • Use pass as a placeholder in an if, loop or function you have not written yet.
  • Check that the lines after else:, elif ...:, try: and except: are indented too.

All Python errors · How to read a traceback · Step through your code