IndentationError: expected an indented block is raised when Python sees a colon (:) that starts a block (e.g. after def, if, for, while, class) but the next line is not indented. In Python, indentation defines block structure; there are no braces. This guide explains the rule and how to fix the error.

What this error means

After a line ending with :, the next logical line must be indented more than the line with the colon. If it is at the same or lesser indentation, Python reports IndentationError: expected an indented block. The error can also point to “unexpected indent” when a line is indented where Python does not expect it. See also TabError when tabs and spaces are mixed, and SyntaxError for other structure issues.

Common causes

  • No indent after colon. You wrote def f(): or if x: and the next line starts at the same column. It must be indented (typically 4 spaces).
  • Empty block. You have nothing (or only comments) after the colon. Python still expects at least one indented statement; use pass if you want an empty block.
  • Mixed tabs and spaces. Python 3 disallows mixing; use spaces only (e.g. 4 spaces per level). TabError is raised when the mix is detected.

Example that causes the error

def greet():
print("Hello")  # IndentationError: body must be indented

if True:
print("Yes")    # Same error

Fix 1: indent the block body

Indent every line that belongs inside the block (usually 4 spaces):

def greet():
    print("Hello")

if True:
    print("Yes")

Fix 2: use pass for empty blocks

If you need a block that does nothing yet, put pass inside it:

def do_nothing():
    pass

if condition:
    pass  # placeholder

Keep indentation consistent

Use the same number of spaces for each level (e.g. 4). Do not mix tabs and spaces. Most editors have a “convert tabs to spaces” option and can show whitespace so you can spot wrong indent.

Next steps: Errors & Exceptions · TabError · SyntaxError · Getting Started