IndentationError: Expected an Indented Block - Python's Annoying Error
Getting IndentationError in Python? Here's why Python hates your spaces and tabs, and how to fix it once and for all.
**IndentationError: expected an indented block**
Python's most annoying error. Especially for beginners.
Why Python Cares About Spaces
Other languages use braces: ```javascript if (true) { console.log('yes'); } ```
Python uses indentation: ```python if True: print('yes') ```
No indentation = error.
Common Mistakes
**1. No indentation after colon** ```python def hello(): print('hi') # Error: expected indent ```
**Fix:** ```python def hello(): print('hi') ```
**2. Mixing tabs and spaces** ```python def hello(): print('hi') # spaces print('bye') # tab - looks same but isn't ```
Python sees these as different. Error.
**Fix:** Use only spaces. Set your editor to convert tabs to spaces.
**3. Wrong amount of spaces** ```python if True: print('1') # 2 spaces print('2') # 4 spaces - Error ```
Be consistent. Use 4 spaces (PEP 8 standard).
**4. Empty function/class** ```python def process(): # will add later
next_function() # Error: process is empty ```
**Fix with pass:** ```python def process(): pass # placeholder ```
VS Code Settings
Add to settings.json: ```json { "editor.tabSize": 4, "editor.insertSpaces": true, "editor.detectIndentation": false } ```
Tab key now inserts 4 spaces.
PyCharm Settings
Settings → Editor → Code Style → Python → Tabs and Indents - Tab size: 4 - Indent: 4 - Use tab character: unchecked
Find Mixed Indentation
Run: ```bash python -m tabnanny yourfile.py ```
Shows where tabs and spaces are mixed.
The Copy-Paste Problem
Copied code from web has weird indentation?
**Quick fix in VS Code:** 1. Select all code 2. Shift + Tab (removes all indentation) 3. Select code blocks 4. Tab (re-indent properly)
Or use auto-format: - Windows/Linux: Shift + Alt + F - Mac: Shift + Option + F
Real Example
I copied code from Stack Overflow: ```python def calculate(x, y): if x > y: return x - y else: return y - x ```
Looked fine. Got IndentationError. Why?
Web used tabs. My editor expected spaces.
**Solution:** ```bash cat -A yourfile.py ```
Shows tabs as ^I. Delete and re-type with spaces.
The Invisible Space
Sometimes you have weird Unicode spaces. Look normal but Python hates them.
Check: ```python # your code here ```
If error on a line that looks fine, delete all spaces on that line and re-add them.
Multi-line Statements
Be careful with backslash: ```python total = 1 + 2 + \ 3 + 4 # must align properly ```
Better use parentheses: ```python total = (1 + 2 + 3 + 4) ```
No backslash needed.
Lists and Dicts
This is fine: ```python my_list = [ 'item1', 'item2', 'item3' ] ```
This too: ```python my_dict = { 'key1': 'value1', 'key2': 'value2' } ```
Python allows any indentation inside brackets.
Class Methods
Common mistake: ```python class MyClass: def my_method(self): # Error: no indent pass ```
**Fix:** ```python class MyClass: def my_method(self): pass ```
Everything inside class must be indented.
My Setup
I use: - 4 spaces always - Editor auto-converts tabs - Auto-format on save - Python extension in VS Code
Never had indentation errors after this setup.
Quick Checklist
- [ ] Use 4 spaces (not tabs) - [ ] Consistent indentation in each file - [ ] Put code after colons (:) - [ ] Use pass for empty blocks - [ ] Enable auto-format in editor
Bottom Line
Python's indentation is annoying at first. Then you realize it makes code cleaner.
Set up your editor properly once. Never worry about it again.