Home › Errors › IndentationError

Python IndentationError: the block structure does not add up

Exception: IndentationErrorCategory: parse time, whitespaceRaised at: before any line runs

What this error means

Python has no { } and no end keyword: indentation is the syntax. The leading whitespace of each line is what tells the parser where a block starts and stops, so whitespace that does not describe a consistent structure is a parse error — IndentationError is a subclass of SyntaxError and, like it, fires before a single statement runs.

There are three wordings, and each one points at a different mistake:

A real example

def average(values):
total = sum(values)
return total / len(values)
  File "stats.py", line 2
    total = sum(values)
    ^
IndentationError: expected an indented block after function definition on line 1

The def line promised a body; line 2 starts in column 0, so the function has no body at all. Indent both lines by four spaces and the module compiles.

Tabs versus spaces: the invisible version

The hardest case is the one you cannot see. A tab and four spaces can look identical in the editor but are different characters, and Python refuses to guess how wide your tab is. Mixing them inside one block raises TabError, a subclass of IndentationError:

  File "stats.py", line 4
    print(total)
                ^
TabError: inconsistent use of tabs and spaces in indentation

This is overwhelmingly a copy-paste wound: code pasted from a blog, a PDF or a chat window brings its original whitespace with it. Two settings make it permanent history — insert spaces instead of tabs and show whitespace — plus PEP 8's four spaces per level, which the whole Python ecosystem already assumes.

How to debug it

  1. Jump to the reported line and compare its left edge with the line above. One of the two is in the wrong column; the message says which direction.
  2. Turn on *show whitespace* (or *render control characters*) in the editor — a tab draws as one arrow, spaces as dots, and the culprit becomes visible.
  3. Re-indent the whole block rather than nudging one line: select it and use the editor's indent command so every line moves by the same amount.
  4. Convert the file once with *Convert indentation to spaces*, then keep four spaces per level everywhere.
  5. If a block legitimately has nothing in it yet, put pass in it — an empty body is still a missing body to the parser.

Fix it interactively

Indentation bugs are best fixed by eye, in a real editor. These free problems do exactly that in the browser:

Practice more Python bugs →

Related errors

More messages and fixes: the Python errors guide. Beginners often meet this one on day one — the Python tutorials hub starts from the basics.

🇮🇳 Hindi में समझें

Python में indentation ही syntax है — { } नहीं होते, इसलिए line के आगे का space ही बताता है कि block कहाँ शुरू और खत्म होता है। expected an indented block = : के बाद वाली line indent नहीं है; unexpected indent = बिना वजह space लगा दिया; unindent does not match = line किसी भी बाहरी block के column से मेल नहीं खाती। Tab और space मिलाओ मत — हर level पर 4 spaces रखो।

पूरी Hindi explanation पढ़ें →