How to Fix: IndentationError: expected an indented block

3D illustration of a staircase with a missing first step causing a robot to stop, representing the expected indented block error.

This is the twin error to IndentationError: unexpected indent. One of the most common Python issues beginners face is IndentationError expected block. It’s one of Python’s most fundamental rules.

This error means: “You used a colon (:), but you didn’t indent the code that came after it.”

โšก Quick Fix: IndentationError: expected an indented block โ€” Python 4-Space Indent Fix for def, if, for, class, try, and Empty Block pass Placeholder

Python found a colon but no indented code on the next line โ€” every block opener demands at least one indented statement directly beneath it.

# WRONG โ€” def body sits at the same level as the def line
def my_function():
print("Hello")        # IndentationError: expected an indented block

# WRONG โ€” if body sits at the same level as the if line
if x > 10:
print("x is big")     # IndentationError: expected an indented block

# WRONG โ€” empty block with only a comment: comments don't count as statements
def function_for_later():
    # I'll write this tomorrow
    # IndentationError: expected an indented block โ€” comment is not code

# RIGHT โ€” indent 4 spaces after every colon
def my_function():
    print("Hello")    # body is indented โ€” no error

if x > 10:
    print("x is big") # body is indented โ€” no error

# RIGHT โ€” use pass as a placeholder for any empty block
def function_for_later():
    pass              # valid empty body โ€” no error, no crash

Every block opener in Python โ€” def, class, if, elif, else, for, while, try, except, finally โ€” requires at least one real statement indented beneath it.

The Cause

In Python, any line that ends with a colon (:) is the start of a new “block” of code. The entire next block must be indented (usually by 4 spaces).

Problem Code:

def my_function():
print("Hello, world!") # CRASH!

Error: IndentationError: expected an indented block

Python sees def my_function(): and expects the next line to be indented, but print() is at the same level.

Common Scenarios

This happens everywhere in Python:

  • if, elif, else
  • for, while
  • def, class
  • try, except

Problem Code (if statement):

if x > 10:
print("x is big") # CRASH!

Problem Code (Empty Block): Sometimes you want to leave a block empty for later. You can’t just leave it blank.

def function_i_will_write_later():
# I'll write this tomorrow
# CRASH!

The Fix

  1. Indent Your Code: Add 4 spaces to the line(s) after the colon.
if x > 10:
    print("x is big") # Correct!

2. Use pass for Empty Blocks: If you want an empty block, use the pass keyword. It’s a placeholder that does nothing, but it satisfies the syntax.

def function_i_will_write_later():
    pass # Correct! No error.

IndentationError: expected an indented block โ€” The Colon Rule and the pass Keyword That Prevents It

IndentationError: expected an indented block enforces one rule: every line ending with a colon must have at least one indented statement beneath it. No exceptions, no empty blocks, no comment-only bodies.

Seven block openers trigger this error when their body is missing or unindented: def, class, if/elif/else, for, while, try/except/finally. Any one of these followed by an empty line, a comment, or code at the same indentation level fires the error immediately.

Two fixes cover every scenario.

Indent the body. Add 4 spaces to every line that belongs inside the block. Python’s standard is 4 spaces โ€” set your editor to insert spaces on Tab keypress and the indentation becomes automatic. Every keystroke after the colon produces exactly what Python expects.

Use pass for empty blocks. pass is a no-op statement โ€” it does nothing, returns nothing, and costs nothing at runtime. It satisfies Python’s block requirement completely. Use it for functions you haven’t written yet, class bodies holding only docstrings, if branches that need no action, and except blocks that swallow an error silently by design.

Every valid empty block pattern

def stub_function(): pass # one-liner form
class EmptyClass: pass
if condition: pass
try:
risky_operation()
except SpecificError:
pass # swallow silently โ€” document why in a comment

The one-liner form (def stub(): pass on a single line) is valid Python and useful for rapid prototyping. For anything beyond a stub, put the body on the next line with 4-space indentation โ€” it’s more readable and easier to extend.

Similar Posts

Leave a Reply