IndentationError: expected an indented block
Educational use only. Content explains errors and defensive fixes for systems you own or are authorised to test. Do not use any technique here to access data, accounts, or networks without permission.
Root Cause
Unlike languages that use curly braces `{}` to define code blocks, Python uses whitespace indentation. This error is a strict syntax error indicating that the Python interpreter expected an indented block of code following a control flow statement (like `if`, `for`, `while`, `def`, or `class`) ending with a colon `:`, but found code at the same or lesser indentation level. It also occurs if you mix tabs and spaces inconsistently within the same file.
Fix / Solution
Ensure that exactly one level of indentation (standard is 4 spaces) follows any statement ending in a colon. If your editor is mixing tabs and spaces, configure it to convert tabs to spaces automatically. You can use tools like `pylint` or `flake8` to catch indentation issues before running the code. If a block is intentionally empty, use the `pass` keyword.
Code Snippet
# ❌ Syntax error: print statement is not indented
def greet():
print("Hello")
# ✅ Correct indentation (4 spaces)
def greet():
print("Hello")
# ✅ Intentionally empty block
if condition:
pass
else:
print("Done")