IndentationError: expected an indented block
What it means
A line that ends with a colon, such as an if, for, while or def line, must be followed by at least one indented line. Python found the next line at the same level as the colon line, so the block is empty. The message names the statement and the line it started on.
1. Forgetting to indent the body of an `if`
This raises the error
Traceback (most recent call last):
File "<exec>", line 3
print("You can vote")
^^^^^
IndentationError: expected an indented block after 'if' statement on line 2The fix
You can vote
Python uses indentation, not brackets, to know which lines belong to the if. Indent them by 4 spaces, the standard.
2. A loop body that is not indented
This raises the error
Traceback (most recent call last):
File "<exec>", line 3
total += n
^^^^^
IndentationError: expected an indented block after 'for' statement on line 2The fix
Total: 27
Indent the lines that should repeat. The print stays at the left edge, so it runs once after the loop finishes.
3. A function with nothing in it yet
This raises the error
Traceback (most recent call last):
File "<exec>", line 4
print("Program started")
^^^^^
IndentationError: expected an indented block after function definition on line 1The fix
Program started
A comment does not count as a statement, so the function is still empty. pass is a statement that does nothing, which fills the block until you write the real code.
When you see this error, check
- Look at the line Python names in the message: it ends with a colon and needs an indented line after it.
- Indent every line of the block by the same amount, usually 4 spaces.
- Use
passas a placeholder in anif, loop or function you have not written yet. - Check that the lines after
else:,elif ...:,try:andexcept:are indented too.
Related errors
All Python errors · How to read a traceback · Step through your code