How to Fix Python Indentation Error: Expected an Indented Block
The "SyntaxError: expected an indented block" is one of the most common Python errors beginners encounter. This error occurs when Python expects indented code after certain statements but finds none.
What Causes This Error? #
Python uses indentation to define code blocks, unlike other languages that use braces {}. When you write statements like if, for, while, def, or class, Python expects the following lines to be indented.
Common Scenarios and Solutions #
1. Empty Function or Class Definition #
Problem:
def my_function():
# SyntaxError: expected an indented block
class MyClass:
# SyntaxError: expected an indented block
Solution:
def my_function():
pass # Use 'pass' as a placeholder
class MyClass:
pass # Use 'pass' for empty classes
2. Missing Indentation After Control Statements #
Problem:
if True:
print("This should be indented")
# SyntaxError: expected an indented block
for i in range(3):
print(i)
# SyntaxError: expected an indented block
Solution:
if True:
print("This should be indented") # 4 spaces indentation
for i in range(3):
print(i) # 4 spaces indentation
3. Accidentally Removed Indentation #
Problem:
def calculate_sum(numbers):
total = 0 # Should be indented
for num in numbers:
total += num
return total
Solution:
def calculate_sum(numbers):
total = 0 # Properly indented
for num in numbers:
total += num
return total
Step-by-Step Fix Process #
Step 1: Identify the Line #
Python's error message shows exactly where the problem occurs:
File "example.py", line 5
print("Hello")
^
SyntaxError: expected an indented block
Step 2: Check the Previous Line #
Look at the line before the error. Does it end with a colon :? If yes, the next line needs indentation.
Step 3: Add Proper Indentation #
- Use 4 spaces (Python standard)
- Avoid mixing tabs and spaces
- Ensure consistent indentation throughout
Step 4: Use 'pass' for Placeholders #
If you're not ready to write the code block yet, use pass:
🐍 Try it yourself
Quick Diagnostic Checklist #
When you encounter this error:
- Does the previous line end with a colon
:? - Is the next line indented with 4 spaces?
- Are you mixing tabs and spaces?
- Is it an empty function/class that needs
pass? - Did you accidentally delete indentation while editing?
Prevention Tips #
- Use a proper code editor that shows indentation levels
- Configure your editor to show whitespace characters
- Set tab size to 4 spaces in your editor
- Use consistent indentation throughout your file
- Always use
passfor empty code blocks
Interactive Example #
Here's a working example that demonstrates proper indentation:
🐍 Try it yourself
Summary #
The "expected an indented block" error is easily fixed by:
- Adding proper 4-space indentation after colon-ending statements
- Using
passas a placeholder for empty blocks - Maintaining consistent indentation throughout your code
- Using a code editor that highlights indentation issues
Remember: Python's indentation isn't just for readability—it's part of the syntax that defines your program's structure.