IndentationError —
Whitespace isn't cosmetic in Python — it's syntax.
Python uses indentation to define code blocks — there are no curly braces marking where an if-statement or function body starts and ends. When the indentation doesn't match what the parser expects, it can't even figure out the structure of your code. Watch the parser reject a mismatched block on LearnBug and see exactly which line broke it.
What is IndentationError?
Python raises this when the indentation of a line doesn't match what the surrounding code structure requires — a block that should be indented isn't, or one line in a block doesn't match the indentation of its siblings. It's caught before your code even starts running, during parsing.
Why visualization helps
Mixed tabs and spaces can look identical in many editors, making the actual problem invisible to the eye. LearnBug (and most good editors) can make whitespace visible or highlight the exact character where indentation breaks, turning an invisible problem into a visible one.
Three ways indentation breaks
A block that should be indented, isn't
The line right after a colon (if, else, for, def) needs at least some indentation — leaving it flush with the statement above raises the error.
Mixed tabs and spaces
Two lines that look identically indented in an editor can actually use different whitespace characters — Python (in Python 3) rejects code that mixes them inconsistently.
Inconsistent indentation within one block
Two lines meant to be siblings in the same block — like two statements inside the same if — use a different number of spaces from each other.
An else block with a missing indent
Python parses the if block correctly — its body is indented properly
The line under if age >= 18: is indented with 4 spaces, exactly as expected — Python correctly identifies this as the if-block's single statement.
Python reaches else: — expects the next line to be indented under it
A colon at the end of a line (from if, else, for, def, etc.) is a structural signal — Python's parser now specifically expects an indented block to follow.
The next line has zero extra indentation — the parser rejects it immediately
This isn't a runtime check — Python can't even finish building a valid structure to run. Indentation isn't optional style here; it's the only thing telling Python where the block is.
Result: IndentationError: expected an indented block ✗ — caught before any code runs
The broken code, and the traceback it produces
def check_age(age):
if age >= 18:
print("You are an adult")
else:
print("You are a minor") # missing indentation under else
check_age(15) File "age_check.py", line 5
print("You are a minor")
^
IndentationError: expected an indented block after 'else' statement on line 4Match the indentation to the if block above it
def check_age(age):
if age >= 18:
print("You are an adult")
else:
print("You are a minor") # now indented to match
check_age(15)Quick Reference
Frequently asked questions
Why does Python use indentation instead of braces like other languages?
It's a deliberate design choice — Python's creator wanted code structure to be visually unambiguous, so the way code looks (its indentation) is forced to match how it actually behaves. This eliminates a whole category of bugs common in brace-based languages, where indentation and actual block structure can silently disagree.
How many spaces should I use per indentation level?
PEP 8, Python's official style guide, recommends 4 spaces per indentation level, and specifically recommends against mixing tabs and spaces. Python 3 actually forbids ambiguous mixtures outright — code that would indent differently depending on tab width is rejected as an error rather than silently guessed at.
Can I use tabs instead of spaces?
You can, as long as you're consistent — but mixing tabs and spaces within the same block is exactly what triggers this error most often, especially when copy-pasting code between editors with different tab settings. Configuring your editor to insert spaces when you press Tab avoids this category of bug entirely.
Why did this error get caught before any of my code ran?
Python has to build a valid internal representation of your code's structure (parsing) before it can execute any of it — and indentation is part of that structure, not just how the code looks. A syntax-level problem like this is caught during parsing, before execution starts, which is why even print statements before the broken line still won't run.
Watch exactly which line breaks the indentation structure
Paste your code into LearnBug to spot indentation issues before you run it elsewhere.