IndentationError: expected an indented block in Python

Quick answer: IndentationError: expected an indented block means Python saw a compound statement such as if, for, def, class, try, or with that needs a suite but found no correctly indented statement. Indent the body or use pass when an empty block is intentional.

Python indentation infographic showing compound header, colon, nested suite, pass, syntax checking, and hidden tab problems
After a colon, Python needs a correctly indented suite; comments alone do not complete a block.

IndentationError: expected an indented block means Python found a statement that must have a child block, but the next line was not indented. This often happens after if, else, for, while, def, class, try, or with.

Why Python raises this error

Python uses indentation to define blocks. Other languages may use braces, but Python requires consistent leading whitespace. If a line ends with :, the next logical line usually needs to be indented.

if True:
print("yes")

This raises an IndentationError because print() belongs inside the if block but starts at the same indentation level.

Fix an if block

Indent the body under the if statement.

if True:
    print("yes")

Use the same indentation style throughout the file. Four spaces per indentation level is the standard Python convention.

Fix a function body

A function definition must have an indented body.

def greet(name):
print(f"Hello, {name}")

Correct version:

def greet(name):
    print(f"Hello, {name}")

greet("Asha")

Python Pool infographic showing indentation, a suite, colon, and nested statements
Code block: Indentation, a suite, colon, and nested statements.

Fix a for loop body

Loops also require an indented block.

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

Correct version:

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

Use pass for an empty block

If you intentionally want an empty class, function, loop, or conditional block, use pass. It is a placeholder statement that tells Python the empty block is intentional.

class User:
    pass

print(User)

Use pass temporarily while drafting code, then replace it with the real implementation later.

Use spaces consistently

Do not mix tabs and spaces. Even if the code looks aligned in an editor, Python may treat tabs and spaces differently. Configure your editor to insert spaces and reformat the file.

def total(values):
    result = 0
    for value in values:
        result += value
    return result

print(total([1, 2, 3]))

This example uses four spaces for each indentation level and keeps the nested for loop clear.

Python Pool infographic showing Python if, loop, function, class, and branch scope
Scope: Python if, loop, function, class, and branch scope.

Where indentation is required

  • After conditionals: if, elif, and else.
  • After loops: for and while.
  • After function and class definitions: def and class.
  • After exception handling blocks: try, except, else, and finally.
  • After context managers: with.
  • After pattern matching blocks: match and case.

Nested blocks need one more indentation level

When blocks are nested, each child block must move one level deeper. This is common when an if statement contains a loop or when a loop contains another conditional.

def report(values):
    if values:
        for value in values:
            print(value)
    else:
        print("no values")

report([1, 2])

The print(value) line is inside the for loop, so it is indented deeper than the for line. The else line aligns with if, not with for.

Editor settings that prevent this error

Most editors can insert spaces automatically when you press Tab. In VS Code, PyCharm, Sublime Text, and similar editors, set the Python indentation size to four spaces and enable format-on-save if your workflow allows it. A formatter cannot fix every syntax error, but consistent editor settings prevent many mixed-whitespace mistakes before they happen.

Python Pool infographic comparing spaces, tabs, editor settings, alignment, and a fix
Fix layout: Spaces, tabs, editor settings, alignment, and a fix.

Quick troubleshooting checklist

  • Look at the line above the error. If it ends with :, indent the following block.
  • Check whether a block is empty. Add pass if it is intentionally empty.
  • Convert tabs to spaces in the editor.
  • Make sure nested blocks are indented one level deeper than their parent.
  • Run a formatter after fixing syntax so spacing stays consistent.

Common related errors

TabError is related to inconsistent tab and space use. IndentationError is the broader indentation problem. Both are subclasses of SyntaxError, so Python raises them before the program runs.

Related Python error guides

Python Pool infographic checking empty suites, nesting, syntax, and formatting
Indent tests: Python Pool infographic checking empty suites, nesting, syntax, and formatting.

Official references

Conclusion

Fix IndentationError: expected an indented block by indenting the required child block, using pass for intentionally empty blocks, and keeping tabs and spaces consistent. In most cases, the line directly above the error tells you which block needs indentation.

Every Compound Header Needs A Suite

After a header ending with a colon, Python expects one or more statements in an indented block. The indentation groups statements into the suite controlled by if, for, while, def, class, try, with, and related statements. A comment alone is not an executable statement, so it does not satisfy the requirement.

def load_report(path):
    # A real implementation belongs here.
    pass

if should_refresh:
    refresh_cache()
else:
    print("Using cached data")

Check Indentation And Hidden Characters

Align the block consistently with spaces and inspect the lines immediately before and after the reported location. Mixing tabs and spaces, indenting one line at the wrong level, or leaving a nested header empty can all produce confusing errors. The caret may point at the first line where Python can prove the structure is invalid rather than the original mistake.

Validate Without Running Application Logic

Compile the file with python -m py_compile file.py or parse source with ast.parse() while debugging syntax. These checks help isolate indentation and parsing errors before imports, network calls, or business logic execute. Keep pass only when the empty block is meaningful; otherwise add the smallest real statement that completes the behavior.

Frequently Asked Questions

What does expected an indented block mean?

Python found a compound statement that needs a suite after its colon but did not find a correctly indented statement.

How do I fix expected an indented block?

Indent the body consistently under the header or add the smallest real statement that completes the intended behavior.

Can I use pass to fix an empty Python block?

Yes. pass is a valid no-op statement when the empty block is intentional and should remain documented.

Does a comment count as an indented block?

No. A comment is ignored by the parser, so an empty compound block still needs pass or another executable statement.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted