All Python errors
IndentationError in Python: wrong indentation
IndentationError means the spaces at the beginning of a line are wrong. To fix it, indent every line that belongs to a block (after a colon) by 4 spaces, and keep lines of the same level exactly aligned.
Code with the error
is_raining = True
if is_raining:
print("Take an umbrella")
Error message (Python 3.12, last line)
Corrected code
is_raining = True
if is_raining:
print("Take an umbrella")
Output
Take an umbrella
Common causes
- “expected an indented block”: nothing is indented after a line ending with a colon.
- “unexpected indent”: a line has extra spaces for no reason.
- “unindent does not match any outer indentation level”: the line is shifted back by the wrong number of spaces.
- Tabs and spaces are mixed in one file.
Practise in the course
Lesson: ConditionsFrequently asked questions
How many spaces should I use?
Four spaces per level is the usual Python style. What matters most is that one block uses the same number.
Is IndentationError a kind of SyntaxError?
Yes. It is a subtype of SyntaxError, so the program does not start until the indentation is fixed.