Paul's Python CoursePPC

What does "IndentationError: expected an indented block" mean?

, 3 minutes to read

It means a line that ends in a colon, such as an if, else, for, while or def, has nothing indented underneath it. Indent the lines that belong to it, usually by four spaces, or if you haven't written them yet, put pass there for now.

Why Python cares about spaces

In Python, indentation is part of the code. The colon at the end of if speed > 300: says a block is about to start, and the indented lines below it are that block. They're the lines that only run when the test is true, or the lines a loop repeats. When the next line isn't indented, Python can't tell what belongs to the if, so it won't run the file at all.

Python 3.14 tells you which statement it was waiting on. This is what you get from the example further down.

  File "main.py", line 3
    print("That's fast.")
    ^^^^^
IndentationError: expected an indented block after 'if' statement on line 2

The error is reported on line 3, the line that should have been indented, and the end of the message points back to the if on line 2 that wanted it. The wording changes to match, so after an else it says after 'else' statement, and after a def it says after function definition.

Notice there's no Traceback (most recent call last) at the top. Python checks the layout of the whole file before it runs anything, so even line 1 never runs.

Run this one to see the error. Then put four spaces at the start of line 3 and run it again, and it prints That's fast. and Done. After that, change 340 to 200. Only Done. is printed this time, because line 4 isn't indented, so it isn't part of the if and runs either way.

Run it

main.py
speed = 340
if speed > 300:
print("That's fast.")
print("Done.")

Output

Press Run and what your program prints shows up here.

When the block is meant to be empty

Sometimes you write the if first and plan to fill it in later. A comment doesn't count as a line of code, so this still gives the same error, reported on line 4.

choice = "2"
if choice == "1":
    # do this later
print("Finished.")

Put pass where the code will go. It's a line that does nothing, and it keeps Python happy until you replace it.

choice = "2"
if choice == "1":
    pass
print("Finished.")

Two related errors are worth recognising. IndentationError: unexpected indent is the opposite problem, a line indented when nothing above it asked for a block. And TabError: inconsistent use of tabs and spaces in indentation means some lines use tabs and others use spaces. The editor on this site puts in four spaces when you press Tab, so you're unlikely to meet that one here.

A small exercise

This program should print Good sailing., but it stops with the error from this page. Fix the indentation.

knots = 20
if knots > 30:
print("Strong wind.")
elif knots > 10:
print("Good sailing.")
else:
print("Too calm.")
Show an answer
knots = 20
if knots > 30:
    print("Strong wind.")
elif knots > 10:
    print("Good sailing.")
else:
    print("Too calm.")

Each print belongs to the line above it, so each one is indented. The elif and else lines stay at the left edge, lined up with the if, because they're part of the same decision.

You'll need this in the Unit Converter project, where Neil's menu has an if, elif and else, with another if inside one of the options, so every line has to sit at the right depth. The Handbook page on if, elif and else shows more examples, and SyntaxError covers the other errors Python finds before a program runs.

The project that goes with it

Unit Converter is a free project in Act 1, for Neil. It runs in your browser with nothing to install, and you don't need an account for it.

More guides