{ }codelper

Error Messages › Python

Python IndentationError

IndentationError: expected an indented block

Python uses the spaces at the start of a line (indentation) to decide what is inside an if or a for. You get this error when those spaces are missing, are there when they should not be, or do not line up with each other. With this error, not a single line of your program runs. Look at the leading spaces on the line that the number after line in the error message points to.

Cause 1. expected an indented block: the inner line is not indented

A line that ends with a colon (:), such as if, for, while or def, must be followed by at least one indented line. The after 'if' statement on line 2 at the end of the error message means the if on line 2 has no inner line (this part appears from Python 3.10 on).

Wrong code

x = 5if x > 3:print("big")
When you run it
  File "main.py", line 3
    print("big")
    ^^^^^
IndentationError: expected an indented block after 'if' statement on line 2

Edit and run it →

Fixed code

Put 4 spaces in front of the inner line.

x = 5if x > 3:    print("big")
Output
big

Edit and run it →

If you have nothing to put inside yet, add a line with pass.

Cause 2. unexpected indent: a line is indented where it should not be

You get this when a line starts with spaces but does not come after a line ending in a colon. It often happens when you copy and paste code from somewhere else and the leading spaces come along with it.

Wrong code

name = "Mina"    print(name)
When you run it
  File "main.py", line 2
    print(name)
IndentationError: unexpected indent

Edit and run it →

Fixed code

Delete the spaces in front of that line so it lines up with the line above.

name = "Mina"print(name)
Output
Mina

Edit and run it →

Cause 3. unindent does not match: the indentation matches no earlier line

When you reduce the indentation to step back out, the line must have the same number of spaces as some earlier line. Below, the last line has 2 spaces, but the lines above have only 0 or 4.

Wrong code

x = 5if x > 3:    print("big")  print("done")
When you run it
  File "main.py", line 4
    print("done")
                 ^
IndentationError: unindent does not match any outer indentation level

Edit and run it →

Fixed code

Use 4 spaces if the line belongs inside the if, or 0 if it belongs outside.

x = 5if x > 3:    print("big")print("done")
Output
big
done

Edit and run it →

Cause 4. TabError: tabs and spaces are mixed

They may look the same on screen, but one tab character and 4 spaces are different. Mixing the two inside one block gives TabError: inconsistent use of tabs and spaces in indentation. TabError is a kind of IndentationError. The last line of the code below is indented with a tab.

Wrong code

x = 5if x > 3:    print("big")	print("done")
When you run it
  File "main.py", line 4
    	print("done")
    ^
TabError: inconsistent use of tabs and spaces in indentation

Edit and run it →

Fixed code

Stick to one of them. Most people use 4 spaces. Delete the indentation at the start of the line and put it in again.

x = 5if x > 3:    print("big")    print("done")
Output
big
done

Edit and run it →

What to check when you see this error

  1. The number after line in the error message is the line with the problem. The error message also shows the code on that line.
  2. For expected an indented block, check that the line above ends with a colon and that the line below it is indented.
  3. For unexpected indent, delete the spaces in front of that line.
  4. For unindent does not match or TabError, delete the indentation on that line and the lines above and below it, then put it back with 4 spaces.
  5. Indentation can be valid and still change the meaning. Choose it by whether the line should run inside the loop or outside it.

Practice problems

See also