Python enforces indentation as a fundamental part of its syntax. Unlike other programming languages that use curly brackets {}
or keywords like begin
and end
to define code blocks, Python relies solely on indentation (spaces or tabs) to indicate block structure. The error IndentationError: expected an indented block occurs when Python expects an indented code block but doesn’t find one.
This guide will break down the IndentationError, why it happens, and how to fix it with detailed explanations and examples.
1. Understanding Indentation in Python
In Python, indentation is used to define code blocks. This includes:
- Functions
- Loops
- Conditional statements (
if
,elif
,else
) - Class definitions
- Exception handling (
try
,except
,finally
)
Basic Example of Indentation
def greet():
print("Hello, World!") # This line is indented correctly.
Here, the print
statement is inside the function greet()
and is correctly indented.
If we remove the indentation, Python will raise an IndentationError:
def greet():
print("Hello, World!") # IndentationError: expected an indented block
2. Common Causes of “expected an indented block” Error
2.1. Missing Indentation
One of the most common causes is forgetting to indent after a statement that requires a block.
Example:
def say_hello():
print("Hello!") # ❌ Error: This line should be indented
Solution:
Indent the print
statement properly.
def say_hello():
print("Hello!") # ✅ Correct: Indented properly
2.2. Improper Indentation After Conditional Statements
Python requires an indented block after if
, elif
, and else
.
Example with Error:
age = 18
if age >= 18:
print("You are an adult.") # ❌ Error: Indentation is missing
Solution:
age = 18
if age >= 18:
print("You are an adult.") # ✅ Correct: Indented properly
2.3. Loops Without Indented Code
Loops (for
, while
) require a properly indented block.
Example with Error:
for i in range(5):
print(i) # ❌ Error: Expected an indented block
Solution:
for i in range(5):
print(i) # ✅ Correct: Indented properly
2.4. Function Definition Without Indented Code
A function must contain an indented block of code.
Example with Error:
def greet():
# No indentation here
print("Hello!") # ❌ Error: Expected an indented block
Solution:
def greet():
print("Hello!") # ✅ Correct: Indented properly
2.5. Class Definitions Without Indented Code
A class requires an indented block after its definition.
Example with Error:
class Person:
pass # ❌ Error: If no implementation, use pass or indent a method.
Solution:
class Person:
def greet(self):
print("Hello!") # ✅ Correct: Indented properly
OR
If you want an empty class, use pass
:
class Person:
pass # ✅ Correct: `pass` prevents indentation error
3. Mixed Spaces and Tabs
Python does not allow mixing tabs and spaces for indentation. If your code has inconsistent indentation, Python raises an error.
Example:
def example():
print("Hello") # Uses spaces
print("World") # ❌ Uses a tab (will cause an error)
Solution:
Make sure to use only spaces or tabs consistently. The recommended way is to use 4 spaces per indentation level.
4. Fixing Indentation Errors Step by Step
- Read the error message carefully.
- Python tells you the line number where the indentation issue exists.
- Check for missing indentation.
- Ensure all function definitions, loops, and conditionals have indented blocks.
- Avoid mixing spaces and tabs.
- Convert all indentation to spaces (most editors do this automatically).
- Use a code editor with indentation support.
- VS Code, PyCharm, and Sublime Text highlight indentation errors.
- Manually re-indent the block.
- Select all code (
Ctrl + A
) and pressShift + Tab
to remove indentation. - Then, press
Tab
or4 spaces
to re-indent properly.
- Select all code (
5. Checking Indentation in Different Editors
- VS Code
- Enable “Render Whitespace” (
Ctrl + Shift + P
→ “Toggle Render Whitespace”) to see spaces and tabs. - Use
Tab
to convert spaces orShift + Tab
to remove indentation.
- Enable “Render Whitespace” (
- PyCharm
- Check indentation settings in
File > Settings > Editor > Code Style > Python
.
- Check indentation settings in
- Sublime Text
- View
Indentation Settings
and convert to spaces.
- View