PythonMastery
reference 3 min read · lesson 51 of 60 in Errors

IndentationError: unexpected indent

1 · The lesson

read

What this error means

A line starts further to the right than Python expected. In Python, indentation is not decoration — it is how a block is opened. Only a line ending in a colon (if, for, def, class, with, try and friends) is allowed to make the next line step in. Anywhere else, a deeper indent is a mistake, and Python refuses to run the file at all.

Nothing in the file has executed when you see this. It is caught while Python is reading the source, before line 1 runs.

When you see it

python
  File "main.py", line 2
    print(order_total)
IndentationError: unexpected indent
python
  File "main.py", line 5
    return total
IndentationError: unexpected indent

Unlike most syntax errors, Python 3.13 prints no ^ arrow here. The line number is the whole clue — and for this error it is reliable: the line it names is the one that is indented too far.

Why it happens

The second traceback above comes from this function, where return is one space deeper than the line before it:

python
def invoice_total(lines):
    total = 0
    for line in lines:
        total += line["price"]
         return total

One space is enough. Python compares each line with the one above: deeper means "new block", and a new block is only legal straight after a colon.

The usual ways to get there:

  • A stray space or two, often from editing a line in a hurry or from an editor that indents by three.
  • A long expression split across lines without brackets — see below.
  • A deleted block header. You removed an if debug: but kept the body underneath it, still indented.
  • Pasted code. A snippet copied out of a function, a chat message or a web page keeps its original indentation, so the first line lands four or eight spaces in at the top level.

The split-expression case catches people who know Python well, because it looks perfectly reasonable:

python
subtotal = 120
shipping = 4.99
total = subtotal + shipping
    + 2.50
print(total)
python
  File "main.py", line 4
    + 2.50
IndentationError: unexpected indent

Without an open bracket, a line break ends the statement. total = subtotal + shipping is complete on its own, so + 2.50 is read as a brand-new line — and a wrongly indented one.

Comments and blank lines never trigger it. Python ignores their indentation, so an indented # note at the top level is fine.

How to fix it

Line it up with its neighbours. If the line belongs to the same block as the line above, give it exactly the same indentation. In the function above, return total belongs after the loop, so it goes back to four spaces:

python
def invoice_total(lines):
    total = 0
    for line in lines:
        total += line["price"]
    return total

order = [{"price": 3.50}, {"price": 4.25}, {"price": 12.00}]
print(f"Invoice total: {invoice_total(order):.2f}")

If the indent was intentional, add the header that opens the block. Indented lines need an owner — usually an if, for, or with that went missing:

python
debug = True
order_total = 120

if debug:
    print(f"order_total = {order_total}")

For a long expression, wrap it in brackets. Inside (), [] or {}, line breaks and indentation stop mattering, which is the idiomatic way to split a line:

python
subtotal = 120
shipping = 4.99
total = (subtotal
         + shipping
         + 2.50)
print(f"Total: {total:.2f}")

A trailing backslash also works, but it breaks the moment someone adds a space after it. Brackets don't.

Let the editor do it. Select the pasted block and press Shift+Tab until it sits where it belongs. Turn on visible whitespace if you suspect a single stray space — it is far quicker than counting.

When you'd actually see this in real code

  • Copying the body of a function out of a Stack Overflow answer and pasting it at the top of a script — every line comes along four spaces deep.
  • Commenting out an if line during debugging and forgetting that its body is still indented underneath.
  • Splitting a long SQL-building expression or a chained calculation across lines without wrapping it in parentheses.
  • A teammate's editor uses two-space indents and yours uses four; one edited line ends up two spaces off from its neighbours.

See Also

Found this useful? Share it with a friend who’s learning Python.