Debugging — Python Error Guide

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.

IndentationErrorException type
Parse timeWhen it's caught
PythonLanguage

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.

LearnBug — the exact line where indentation breaks
🖼 Add a LearnBug screenshot here
What Causes It

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.

Missing indent

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.

Tabs vs spaces

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.

Sibling mismatch
Walkthrough

An else block with a missing indent

1

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.

if age >= 18:
print(...)correctly indented
if-block body: correctly indented, no issue
2

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.

else:expects indented block next
Parser state: expecting an indented block after else:
3

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

Code Example

The broken code, and the traceback it produces

PythonBroken — missing indentation under else
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)
TracebackWhat Python actually prints
  File "age_check.py", line 5
    print("You are a minor")
    ^
IndentationError: expected an indented block after 'else' statement on line 4
The Fix

Match the indentation to the if block above it

PythonFixed — consistent 4-space indentation throughout
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

Error classIndentationErrorA subclass of SyntaxError, specific to whitespace structure
Typical causeMissing or mismatched indentA block body not indented, or mixed tabs/spaces
Fix patternConsistent 4-space indentationPEP 8 (Python's style guide) recommends 4 spaces per level, never tabs
Prevention tipShow whitespace in your editorMost editors can render spaces/tabs visibly and auto-convert tabs to spaces

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.

Open Playground →