IndentationError in Python: Causes and Fixes
Fix IndentationError by understanding what the error means, why Python requires indentation, and how to correct common spacing mistakes in your code.
Quick fix
if True:
print("Correct indentation")
for i in range(3):
print(i)
def greet():
print("Hello")
After a line that ends with a colon (:), indent the next block consistently. Use the same indentation style throughout the file.
What this error means
Python uses indentation to define code blocks.
That means spaces at the start of a line are not just for formatting. They tell Python which lines belong inside an if, for, while, def, class, try, and similar statements.
An IndentationError means the spacing at the start of one or more lines is invalid.
Common examples:
- A line should be indented, but it is not
- A line is indented when no block is open
- One line uses different indentation from the rest of the block
If you are new to this, see Python indentation rules and why they matter.
Why Python raises IndentationError
Python raises this error when it cannot understand the structure of your code.
This usually happens because:
- A block is required after a line ending with a colon (
:) - The next line is not indented when it should be
- A line is indented too much or too little
- Tabs and spaces are mixed in the same file
- One block uses inconsistent indentation levels
Sometimes the real problem is on the line above the one shown in the error. For example, a missing colon can make the next indented line fail. See how to fix SyntaxError: missing colon.
Common situations that cause it
These are the most common causes of IndentationError:
- Forgetting to indent code inside an
ifstatement - Forgetting to indent code inside a function
- Pasting code from another source with mixed tabs and spaces
- Adding extra spaces to a line that should align with nearby lines
- Removing indentation from one line in a loop or conditional block
Other common causes include:
- Missing indentation after
if,for,while,def,class,try, orexcept - Extra indentation on a line outside a block
- Mixed tabs and spaces
- Inconsistent indentation width in the same block
- Copy-pasted code with hidden whitespace differences
- Missing colon before the intended block
Example: missing indentation
A very common error is forgetting to indent the line after a statement ending with :.
Wrong code
if True:
print("Hello")
Python expects the print("Hello") line to belong to the if block, but it is not indented.
This often causes:
IndentationError: expected an indented block
Correct code
if True:
print("Hello")
Use four spaces for the indented line.
If you want a focused explanation of this version of the error, see IndentationError: expected an indented block.
Example: unexpected indentation
This happens when a line starts with spaces even though Python is not currently inside a block.
Wrong code
print("Start")
print("Oops")
print("End")
The second line is indented, but there is no if, for, def, or other block before it.
This can cause:
IndentationError: unexpected indent
Correct code
print("Start")
print("Oops")
print("End")
Remove the extra spaces so the line lines up correctly.
For more help with this exact message, see IndentationError: unexpected indent.
Example: mixed tabs and spaces
Code can look aligned on the screen but still fail if some lines use tabs and others use spaces.
Problem example
def greet():
print("Hello")
print("World")
In this example:
- One indented line may use a tab
- Another indented line uses spaces
Even if they look similar, Python treats them differently.
Better version
def greet():
print("Hello")
print("World")
Use spaces consistently.
Helpful tips:
- Convert tabs to spaces in your editor
- Turn on visible whitespace if your editor supports it
- Use auto-formatting when possible
If your indentation levels no longer line up correctly, you may also see IndentationError: unindent does not match any outer indentation level.
How to fix IndentationError step by step
Use this checklist:
- Read the error line number carefully.
- Check the line above it for a missing colon.
- Check whether the next line should be indented.
- Compare indentation with nearby lines in the same block.
- Replace tabs with spaces.
- Use one consistent indentation size, usually four spaces.
A simple debugging workflow:
python your_script.py
python -m py_compile your_script.py
What these do:
python your_script.pyruns the file and shows the errorpython -m py_compile your_script.pychecks whether Python can compile the file
If you are still stuck, a beginner-friendly guide to debugging Python code can help you track down the problem.
How to prevent this error
You can avoid most indentation problems with a few habits:
- Use an editor that highlights indentation problems
- Enable visible whitespace if available
- Auto-format your code when possible
- Do not mix tabs and spaces
- Indent blocks immediately after typing a colon
A good general rule is:
- Use four spaces for each indentation level
- Keep all lines in the same block aligned exactly
If you want to understand the bigger picture, read Python syntax basics explained.
Related errors
IndentationError often appears in one of these forms:
IndentationError: unexpected indentIndentationError: expected an indented blockIndentationError: unindent does not match any outer indentation levelSyntaxError: missing colon
These messages are closely related because they all affect how Python reads the structure of your code.
FAQ
What is the fastest way to fix IndentationError?
Check the error line and the line above it, then make sure the block uses consistent indentation with spaces.
Should I use tabs or spaces in Python?
Use spaces. Four spaces per indentation level is the common standard.
Can a missing colon cause IndentationError?
Yes. If Python does not see the colon that starts a block, the next indented line may trigger an indentation-related error.
Why does copied code cause indentation problems?
Copied code may contain tabs, different spacing widths, or hidden whitespace that does not match your file.