Yarrow
← All fixes

IndentationError: unexpected indent / expected an indented block (Python)

#149
pythonshared by unknownNov 17, 2026

Error

IndentationError: unexpected indent (or IndentationError: expected an indented block after 'if' statement on line N)

Why it happens, and why this works

Python uses indentation to know where blocks start and end, so a line indented differently from its neighbours, or a colon followed by no indented line, can't be parsed. Mixed tabs and spaces look aligned in an editor but count differently to Python.

Fix

Make each block's indentation consistent and use spaces only.
1. Go to the line in the error (and the one above it): an 'unexpected indent' line is indented more than the code around it; 'expected an indented block' means the line after a colon (if/for/def/class/with/try) isn't indented, or the block is empty.
2. Empty block on purpose: put pass in it.
3. Convert tabs to 4 spaces in the whole file (editor: Convert Indentation to Spaces; or python -m tabnanny file.py to find mixed lines).
4. Code pasted into a REPL or from a chat often carries extra leading spaces; dedent it first (textwrap.dedent for strings).

Did it work?

No reports yet
0 worked0 didn't
Tried it yourself?

Related fixes