Write Python. Understand every error.

Open the editor

Error guide · IndentationError

IndentationError: unindent does not match any outer indentation level

A line steps back to a position that no earlier line used.

Example

def check(x):
    if x > 0:
        print("positive")
      print("done")

What Python says

IndentationError: unindent does not match any outer indentation level

As printed by Python 3.12. Other versions may word it slightly differently.

Try this code in Py-CRAFT

Why it happens

When you leave a block, the next line must line up exactly with an earlier level, such as the if or the def. Here the last line uses 6 spaces, but only 0, 4 and 8 exist above it. Python cannot tell which block it belongs to.

How to fix it

def check(x):
    if x > 0:
        print("positive")
    print("done")

check(5)

Output: positive ⏎ done

Tips

Learn the ideas behind it

Other common errors

Share this page: