Tutorials Logic, IN info@tutorialslogic.com

Python IndentationError: Missing Blocks, Unexpected Indent, and Tabs vs Spaces

Python IndentationError

IndentationError is raised by the parser when Python sees a block that is missing, misaligned, or mixed between tabs and spaces.

Focus on the parser's block rules, the three common error messages, and how to spot the exact line where indentation breaks.

What IndentationError Means

The IndentationError is one of the most common Python errors for beginners. It occurs when Python encounters incorrect or inconsistent indentation in your code. Unlike most programming languages that use braces {} for code blocks, Python uses indentation to define code structure.

Indentation Errors

  • Missing indentation after colon (:) in if, for, while, def, class statements
  • Mixing tabs and spaces for indentation
  • Inconsistent indentation levels (2 spaces vs 4 spaces)
  • Extra or unexpected indentation
  • Copy-pasting code with different indentation styles

Fix Bad Indentation

Add the Missing Block Indent

Add the Missing Block Indent
# Bad indent: print is not inside the if block
if x > 5:
print("Greater")  # Missing indentation!

# Fixed indent: print belongs to the if block
if x > 5:
    print("Greater")

# Configure your editor:
# - Use 4 spaces per indentation level
# - Convert tabs to spaces
# - Enable "show whitespace" to see indentation

Indentation Cases

  • The most common cause - forgetting to indent code after statements that require a code block.
  • Python 3 doesn't allow mixing tabs and spaces. This often happens when copying code from different sources.
  • Using different numbers of spaces for indentation at the same level causes this error.
  • Adding indentation where it's not expected, usually at the start of a file or after a complete statement.
  • Python requires at least one statement in every code block. Use pass for empty blocks.

Function Body Not Indented

Function Body Not Indented
def greet(name):
print(f"Hello, {name}")  # IndentationError!

if age >= 18:
print("Adult")  # IndentationError!

for i in range(5):
print(i)  # IndentationError!

Indent the Function Body

Indent the Function Body
def greet(name):
    print(f"Hello, {name}")  # Properly indented

if age >= 18:
    print("Adult")  # Properly indented

for i in range(5):
    print(i)  # Properly indented

Nested Block at the Wrong Level

Nested Block at the Wrong Level
def calculate():
    x = 10  # 4 spaces
	y = 20  # Tab character - IndentationError!
    return x + y

Align the Nested Block

Align the Nested Block
def calculate():
    x = 10  # 4 spaces
    y = 20  # 4 spaces (consistent)
    return x + y

# Configure your editor to convert tabs to spaces
# VS Code: "editor.insertSpaces": true
# PyCharm: Settings -> Editor -> Code Style -> Python -> Use tab character: unchecked

Mixed Tabs and Spaces

Mixed Tabs and Spaces
def process_data():
    if True:
        print("Start")  # 4 spaces
      print("Middle")  # 2 spaces - IndentationError!
        print("End")    # 4 spaces

Use One Indentation Style

Use One Indentation Style
def process_data():
    if True:
        print("Start")  # 4 spaces
        print("Middle")  # 4 spaces (consistent)
        print("End")    # 4 spaces

Unexpected Indent at File Start

Unexpected Indent at File Start
print("Hello")  # Unexpected indent at file start!

x = 10
    y = 20  # Unexpected indent after complete statement!

Start Top-Level Code at Column One

Start Top-Level Code at Column One
print("Hello")  # No indentation at file start

x = 10
y = 20  # Same indentation level

Empty Function Without pass

Empty Function Without pass
def todo_function():
    # Will implement later
# IndentationError: expected an indented block

if condition:
# IndentationError!

Mark the Empty Block Explicitly

Mark the Empty Block Explicitly
def todo_function():
    # Will implement later
    pass  # Use pass for empty blocks

if condition:
    pass  # Placeholder

# Or use ellipsis (Python 3+)
def another_function():
    ...  # Also valid for empty blocks

Consistent Indentation

  • Use 4 spaces per indentation level - PEP 8 standard for Python code
  • Never mix tabs and spaces - Configure your editor to convert tabs to spaces
  • Enable "show whitespace" in your editor - Makes indentation visible
  • Use a Python-aware editor - VS Code, PyCharm, or Sublime with Python plugins
  • Run python -tt script.py - Detects tab/space mixing issues
  • Use autopep8 or black - Auto-format your code to fix indentation
  • Be consistent - Stick to one indentation style throughout your project

Debug IndentationError

  • Check the line before the error for a colon that requires an indented block.
  • Convert tabs to spaces and keep one indentation width in the whole file.
  • Align sibling statements at exactly the same indentation level.
  • Use pass only when an empty block is intentional and add the real code before release.
IndentationError checkpoint

Can You Fix Python Indentation?

5 checks
  • The IndentationError is one of the most common Python errors for beginners.
  • It occurs when Python encounters incorrect or inconsistent indentation in your code.
  • Unlike most programming languages that use braces {} for code blocks, Python uses indentation to define code structure.
  • After a block-opening statement, the next logical line must be indented consistently.
  • Python 3 doesn't allow mixing tabs and spaces.

Questions About IndentationError

IndentationError occurs when code has incorrect or inconsistent indentation, such as missing indentation after colons, mixing tabs and spaces, or using inconsistent spacing levels.

Always use spaces (4 spaces per level recommended by PEP 8). Python 3 doesn't allow mixing tabs and spaces. Configure your editor to convert tabs to spaces automatically.

Add proper indentation (4 spaces) after statements that require code blocks (if, for, while, def, class). If the block is intentionally empty, use pass or ... with a clear reason.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.