Home › Errors › IndentationError
IndentationError: expected an indented block
Exception: IndentationErrorCategory: compile time (parsing)Raised at: the first line after a block header that is not indented
What this error means
A block header — if, for, while, def, class, try, else — ended with a colon, and Python then expected at least one indented statement belonging to that block. It found none.
This is a compile-time error, so nothing in the file ran. Python parses the whole file into bytecode before executing a single line.
Traceback (most recent call last):
File "demo.py", line 4
print("ready")
^
IndentationError: expected an indented block after 'if' statement on line 3
Python 3.10+ names the exact header, which makes this one of the easier messages to act on: it tells you both the line the body was missing under and the line where the parser gave up.
The four causes
# 1 — the body is simply missing
if ready:
print("ready") # ❌ not indented
if ready:
print("ready") # ✅
# 2 — the body was deleted while editing
def main():
# ❌ nothing left under the header
# 3 — a comment is not a statement
for x in items:
# TODO: handle x # ❌ a comment does not count as a body
pass # ✅ pass does
# 4 — the body is on the header line but a colon is missing
if ready print("ready") # ❌ SyntaxError, not IndentationError
if ready: print("ready") # ✅ legal, but write the block form
Cause 3 surprises people: Python counts *statements*, and a comment is not one. A block containing only comments is still an empty block.
The fix: pass
pass is a statement that does nothing. It exists precisely to fill a block that is intentionally empty:
def handler(event):
pass # ✅ intentionally empty for now
class Placeholder:
... # ✅ the ellipsis literal works too
for x in items:
if x < 0:
pass # ✅ skip negatives, handle the rest below
process(x)
try:
risky()
except ValueError:
pass # ✅ swallow deliberately — but say why
pass and ... are interchangeable here. ... reads better in stub code and type definitions; pass is the conventional choice inside function bodies. Neither does anything at runtime.
One warning about except: pass. Silently swallowing an exception hides real bugs, and you will debug it later without any clue where it started. If you must ignore it, name the exception and write a comment saying why.
One-liner versus block form
Python allows a single statement on the same line as the header. It is legal, and it is also a common source of this error when someone half-converts between the two forms:
if ready: print("ready") # ✅ one statement, same line
if ready: # ❌ the colon promises a block that never comes
if ready:
print("ready") # ✅ the form to prefer
Prefer the block form. The one-liner saves a line and costs you the ability to add a second statement later without restructuring — and it is the form most style guides discourage.
Tabs versus spaces
A closely related failure, with a different message. If the block is indented but the indentation is inconsistent, you get TabError instead:
def f():
if x: # tab-indented
pass # space-indented — ❌ TabError: inconsistent use of tabs
# and this produces IndentationError even though it looks aligned:
def f():
if x: # 2 spaces
pass # 4 spaces — the block is deeper, which is fine,
return x # but mixing levels across the file confuses readers
The rule is simple: pick one style and use it everywhere. PEP 8 specifies four spaces per level and no tabs. Every editor can be told to insert spaces when you press Tab, and to convert an existing file in one command.
- In VS Code: bottom status bar shows the current indentation; click it and choose *Convert Indentation to Spaces*.
- In Vim:
:set expandtabthen:retab. - From the command line:
python -m tabnanny yourfile.pyreports every mixed line with its number.
Related messages worth knowing
- IndentationError: the parent guide — the whole family of indentation failures and how Python decides blocks.
- IndentationError: unexpected indent — the mirror image — a line indented when no block was opened.
- SyntaxError: unexpected EOF while parsing — what you get on Python 3.9 and earlier when the empty block is at the end of the file.
🇮🇳 Hindi में समझें
IndentationError: expected an indented block ka matlab hai kisi block header (if, for, while, def, class, try) ke baad colon tha, lekin uske neeche indented statement hi nahi mila. Python ko kam se kam ek statement chahiye. Ye compile-time error hai, matlab file ka koi bhi code chala hi nahi. Char wajah: (1) body likhi hi nahi — if ready: ke neeche print(...) bina indent ke. (2) Editing ke dauraan body delete ho gayi. (3) Sirf comment likha hai — comment statement nahi hota, isliye wo empty block hi maana jaata hai; pass likhna padta hai. (4) Body usi line par likhni thi par colon chhoot gaya. Fix hai pass — ek aisa statement jo kuch nahi karta, bas intentionally khaali block bharne ke liye hai. ... (ellipsis) bhi kaam karta hai. Ek cheez ka dhyan rakho: except: pass asli bugs chhupa deta hai, to exception ka naam likho aur comment me wajah batao. Agar block indented hai par indentation inconsistent hai (tab aur space mix), to TabError milega — alag message. Rule simple hai: ek style chuno aur hamesha wahi use karo. PEP 8 chaar spaces per level kehta hai, tabs nahi. python -m tabnanny yourfile.py har mixed line bata deta hai.