IndentationError: unindent does not match any outer indentation level (Fix)

Fix the Python error IndentationError: unindent does not match any outer indentation level. This error usually means one line is indented differently from the surrounding block. On this page, you will learn what the error means, why it happens, how to find the bad indentation, and how to fix it.

Quick fix

Use one indentation style consistently. For most beginners, the safest choice is 4 spaces per indentation level. Do not mix tabs and spaces.

if True:
    print("Hello")
    print("World")
print("Done")

In this example:

  • Both print() lines inside the if block use the same indentation.
  • print("Done") is moved back to the left to end the block.
  • That unindent is valid because it matches an earlier indentation level.

If you are not fully sure how Python blocks work, see Python indentation rules and why they matter.

What this error means

Python uses indentation to group code into blocks.

This error appears when a line is unindented to a level that does not match any earlier block.

In simple terms:

  • the line starts too far left, or
  • it uses a different indentation pattern than Python expects.

Python does not use indentation just for style. It uses it to understand your program structure.

For example, in code like this:

if True:
    print("A")
print("B")

print("A") belongs to the if block, and print("B") does not.

Why it happens

This error often happens for one of these reasons:

  • A line is moved back, but not to a valid previous indentation level.
  • Tabs and spaces are mixed in the same block.
  • One line uses 2 spaces and another uses 4 spaces.
  • Code was copied from another editor and kept hidden indentation characters.
  • A block inside if, for, while, try, except, def, or class is not aligned correctly.

Common causes include:

  • Mixing tabs and spaces
  • Using inconsistent numbers of spaces
  • Unindenting to the wrong level
  • Pasting code with hidden indentation characters
  • Editing nested blocks without rechecking alignment

Example that causes the error

Here is a small example with bad indentation:

if True:
    print("Hello")
  print("World")

This can raise:

IndentationError: unindent does not match any outer indentation level

Why?

  • The second print() line is moved left.
  • But it does not return to a valid earlier indentation level.
  • It uses 2 spaces instead of matching the existing block structure.

A line like this can look almost correct in some editors. That is why indentation errors are frustrating for beginners.

Python checks the actual indentation characters, not just how the code looks on screen.

Here is the corrected version:

if True:
    print("Hello")
    print("World")

Both lines now use the same indentation, so they are in the same block.

How to fix it

To fix this error:

  • Make all lines in the same block use the same indentation.
  • Use 4 spaces for each indentation level.
  • Replace tabs with spaces in your editor.
  • Re-indent the whole block instead of fixing only one line.
  • Check the line above the error, because the real problem is often nearby.

Example with a nested block:

for i in range(2):
    if i == 0:
        print("zero")
    print("done with this loop")

This works because:

  • if i == 0: is inside the for loop.
  • print("zero") is inside the if block.
  • print("done with this loop") is unindented back to the for loop level, which is valid.

If one of those lines used a different number of spaces, Python could raise an indentation error.

Step-by-step debugging checklist

When you see this error, use this checklist:

  1. Find the line number in the error message.
  2. Look at the block above that line.
  3. Compare the indentation of nearby lines that should be at the same level.
  4. Delete the indentation on the bad line and type it again manually.
  5. Turn on whitespace or invisible character display in your editor.
  6. Convert all tabs to spaces if needed.

You can also run your script from the terminal:

python your_script.py
python -tt your_script.py
python3 your_script.py

Notes:

  • python your_script.py runs the script normally.
  • python3 your_script.py is often used on systems where Python 3 is available as python3.
  • python -tt your_script.py can help detect tab-related indentation problems more strictly in some environments.

If you are working through several errors, a beginner-friendly next step is how to debug Python code.

Common places beginners see this error

Beginners often see this error:

  • Inside nested if statements
  • After a for or while loop
  • Inside functions defined with def
  • In try-except blocks
  • When pasting code from websites or chat apps

Example inside a function:

def greet():
    print("Hello")
   print("World")

The second line is not aligned correctly with the first line in the function body.

Correct version:

def greet():
    print("Hello")
    print("World")

How editors can help

Your editor can prevent many indentation problems.

Useful settings:

  • Enable visible whitespace
  • Set tab size to 4 spaces
  • Use auto-formatting if your editor supports it
  • Configure the editor to insert spaces when you press Tab

If you copy code from somewhere else, it is often safer to:

  • select the whole block
  • remove the indentation
  • indent it again consistently

This error is closely related to other indentation problems:

Sometimes indentation problems appear after a syntax mistake higher up, such as a missing colon after if, for, or def. See SyntaxError: missing colon if that sounds familiar.

FAQ

What does "unindent" mean in Python?

It means moving a line back to the left to end a block or return to an earlier indentation level.

Can tabs cause this error?

Yes. Mixing tabs and spaces is one of the most common causes.

How many spaces should I use in Python?

Use 4 spaces for each indentation level. This is the standard and the easiest choice for beginners.

Why does the error point to one line when the problem is somewhere else?

Python notices the mismatch when it reaches a line with invalid indentation, but the real cause may be in the block above it.

How do I stop this error from happening again?

Set your editor to insert spaces instead of tabs, keep indentation consistent, and use auto-formatting when possible.

See also