IndentationError: expected an indented block
In Python, the spaces at the start of a line have meaning. They show which lines belong to an if, a loop or a function. An IndentationError means those spaces do not make sense to Python. It is a kind of syntax error, so the program does not start at all until it is fixed.
Case 1: expected an indented block
marks = 72
if marks >= 33:
print("Pass")What Python says
File "example.py", line 3
print("Pass")
^^^^^
IndentationError: expected an indented block after 'if' statement on line 2After a line that ends with a colon, the next line must be indented. Here the print sits at the left edge, so the if has no body. Add four spaces before print.
Case 2: unexpected indent
name = "Asha"
print(name)What Python says
File "example.py", line 2
print(name)
IndentationError: unexpected indentThis is the opposite. The second line is indented although nothing above it opened a block. Remove the spaces. This often happens when code is pasted from a web page or a chat message.
Case 3: the indent does not match
for i in range(3):
print(i)
print("next")What Python says
File "example.py", line 3
print("next")
^
IndentationError: unindent does not match any outer indentation levelThe first body line has eight spaces and the second has four. When a block ends, the next line must go back to exactly an indentation level used before: here, either eight spaces to stay in the loop or none to leave it. Four matches neither.
Case 4: tabs mixed with spaces
A tab and four spaces can look the same on screen but are different characters. If one line of a block starts with a tab and the next with spaces, Python raises a TabError, a close cousin of this error. The cure is to use spaces only. Nearly every code editor can be set to insert four spaces when you press the Tab key, and most are set that way already.
The rules in short
- Use four spaces for each level. This is the custom everyone follows.
- Every line in the same block has the same indentation.
- A line ending in a colon must be followed by a more indented line.
- Never indent a line just to make it look nice.
When the indentation is wrong but there is no error
This is the more dangerous case. The program runs, but does something different from what you meant.
total = 0
for n in [10, 20, 30]:
total += n
print("Running total:", total)
print("Final:", total)Output
Running total: 10
Running total: 30
Running total: 60
Final: 60Move the first print to the left edge and it runs once, after the loop, instead of three times. Python cannot know which you wanted. When a loop prints too often or too rarely, or an if seems to be ignored, check the indentation of the lines around it first.
An empty block
Sometimes you want to write the outline of a program and fill in a block later. An empty block is not allowed, but the word pass stands in for "nothing here yet".
def not_ready_yet():
pass
print("The outline runs without error")Output
The outline runs without errorA quick way to fix a messy file
If a file has become a mixture of tabs and odd numbers of spaces, do not fix it line by line. Select everything and use your editor's command to convert the indentation to spaces. Then go through the blocks one by one from the top, making each level four spaces deeper than the one outside it.
Other error messages
Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.
This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.