IndentationError: expected an indented block (Fix)

Python shows IndentationError: expected an indented block when it reaches a line that should contain a code block, but the next line is not indented.

This usually happens after lines like if, for, while, def, class, try, or else.

Quick fix

if True:
    print("This line is indented correctly")

Python expects an indented block after lines like if, for, while, def, class, try, and else. Add indentation, usually 4 spaces.

What this error means

This error means:

  • Python found a line that starts a block, but no indented code came after it
  • A block is a group of lines inside if, for, while, def, class, try, except, else, and similar statements
  • Python uses indentation to define code structure

In Python, indentation is not optional. It is part of the syntax.

For example, after this line:

if age >= 18:

Python expects the next line to be indented. If it is not, Python raises this error.

If you are new to this, see Python indentation rules and why they matter.

When this error happens

This error often appears:

  • After an if statement with no indented body
  • After a function definition with no indented body
  • After loops like for or while with no indented code
  • After try, except, else, or finally with no body
  • When indentation is missing because of a copy-paste or edit mistake

Here are a few examples of code that can trigger it:

if True:
print("Hello")
def greet():
print("Hi")
for number in [1, 2, 3]:
print(number)

In each case, the line after the colon should be indented.

Example that causes the error

Here is a simple example:

if True:
print("Hello")

The problem is the line after the colon.

  • if True: ends with a colon
  • The colon tells Python that a block is starting
  • The next line must be indented
  • print("Hello") is not indented, so Python raises an error

Correct version:

if True:
    print("Hello")

Output:

Hello

This same rule applies to Python if statements and Python functions.

How to fix it

Use these steps:

  • Indent the next line with 4 spaces
  • Make sure all lines inside the block use the same indentation level
  • If the block is intentionally empty, use pass
  • Check the line above the error, not only the line shown in the message

Example fix

Broken code:

def greet():
print("Hello")

Fixed code:

def greet():
    print("Hello")

greet()

Output:

Hello

Check the line above the error

Sometimes the error message points to a line that looks fine. The real issue is often the line before it.

Example:

if True:
name = "Sam"

Python complains because name = "Sam" should be inside the if block.

Fixed version:

if True:
    name = "Sam"

print(name)

Output:

Sam

Use pass for an empty block

pass is a placeholder that does nothing.

It is useful when you want to write the structure first and add code later.

Common places to use it:

  • After def
  • After class
  • Inside if
  • Inside loops during early drafting

Example:

def greet():
    pass

This code is valid, even though the function does nothing yet.

Another example:

if True:
    pass

Without pass, Python would raise IndentationError: expected an indented block.

Debugging checklist

When you see this error, check these things:

  • Find the line that ends with a colon
  • Check whether the next line is indented
  • Use spaces consistently instead of mixing tabs and spaces
  • Make sure your editor did not remove indentation automatically
  • Run the file again after fixing one block at a time

You can also test your file from the command line:

python your_file.py

Or check for syntax and indentation problems without running the program:

python -m py_compile your_file.py

If the problem is not missing indentation but inconsistent indentation, you may be dealing with IndentationError: unindent does not match any outer indentation level.

This error is not the same as other indentation errors.

IndentationError: unexpected indent

This happens when a line is indented even though Python was not expecting a block there.

Example:

print("Start")
    print("Too much indentation")

See IndentationError: unexpected indent for that case.

IndentationError: unindent does not match any outer indentation level

This happens when indentation levels do not line up correctly, often because tabs and spaces were mixed.

All of these errors usually come from block structure or inconsistent whitespace.

Also check for SyntaxError: missing colon, because forgetting a colon can lead to confusion around block structure.

Common causes

The most common causes are:

  • Forgetting to indent after a colon
  • Leaving a function or if block empty
  • Deleting the only line inside a block
  • Mixing tabs and spaces
  • Bad paste formatting from another editor or website

A common beginner mistake is writing the structure first:

def calculate_total():

This is incomplete. Python expects an indented block after the function line.

To make it valid for now, add pass:

def calculate_total():
    pass

FAQ

What does "expected an indented block" mean in Python?

It means Python expected code inside a block, but the next line was not indented.

How many spaces should I use for indentation in Python?

Use 4 spaces per indentation level. This is the standard Python style.

Can I leave an if statement or function empty?

Yes, but you must use pass as a placeholder inside the block.

Why does the error point to a line that looks correct?

The real problem is often the line above it, especially one that ends with a colon.

See also