IndentationError occurs when Python finds incorrect indentation in the code. Since Python uses indentation (spaces or tabs) to define code blocks, even a small alignment mistake can prevent the program from running. Unlike many other programming languages, indentation in Python is not optional it is a required part of the syntax.
def check_number(a):
if a > 2:
return "Greater than 2"
a = 5
print(check_number(a))
Output
ERROR!
Traceback (most recent call last):
File "<main.py>", line 2
if a > 2:
^^
IndentationError: expected an indented block after function definition on line 1
Explanation: statement if a > 2: should be indented inside the function definition. Since Python cannot determine which statements belong to the function body, it raises an IndentationError.
Common Causes
1. Missing Indentation After a Statement: Python expects an indented block after statements such as if, for, while, def and class.
age = 18
if age >= 18:
print("Eligible to vote")
Output
ERROR!
Traceback (most recent call last):
File "<main.py>", line 4
print("Eligible to vote")
^^^^^
IndentationError: expected an indented block after 'if' statement on line 3
Explanation: print() statement should be indented because it belongs to the if block.
2. Mixing Tabs and Spaces: Using tabs and spaces together often creates inconsistent indentation.
def display():
print("Hello")
print("Python")
Output
ERROR!
Traceback (most recent call last):
File "<main.py>", line 3
print("Python")
^
IndentationError: unindent does not match any outer indentation level
Explanation: One line is indented using a tab while another uses spaces. Python treats them differently and cannot determine the correct block structure.
3. Unexpected Extra Indentation: Adding unnecessary indentation where it is not required can also cause an error.
name = "John"
print(name)
Output
ERROR!
Traceback (most recent call last):
File "<main.py>", line 3
print(name)
IndentationError: unexpected indent
Explanation: print() statement is indented even though it does not belong to any code block.
Fixing IndentationError
1. Use Proper Indentation: Ensure that all statements inside a block are indented consistently.
def check_number(a):
if a > 2:
return "Greater than 2"
return "2 or less"
a = 5
print(check_number(a))
Output
Greater than 2
Explanation: Each statement is correctly indented according to its block level, so Python can interpret the code without errors.
2. Use Consistent Spaces: PEP 8 recommends using four spaces for each indentation level.
for i in range(3):
print(i)
Output
0 1 2
Explanation: All statements inside the loop use the same indentation level, making the code valid and easy to read.
3. Use an IDE or Code Editor: Modern editors such as VS Code, PyCharm and Jupyter Notebook automatically manage indentation and highlight indentation-related issues.
def greet():
print("Hello")
Explanation: Using an editor with auto-indentation helps reduce the chances of indentation mistakes and improves code readability.
Best Practices
- Use 4 spaces for each indentation level.
- Avoid mixing tabs and spaces in the same file.
- Enable auto-formatting in your code editor.
- Keep indentation consistent throughout the program.
- Use IDEs that highlight indentation problems before execution.