Error guide · IndentationError
IndentationError: expected an indented block
The spaces at the start of a line are wrong for the structure of the code.
Example
for i in range(3): print(i)
What Python says
IndentationError: expected an indented block after 'for' statement on line 1
As printed by Python 3.12. Other versions may word it slightly differently.
Why it happens
Python uses indentation (spaces at the start of a line) to know which lines belong inside a loop, if or function. After a line ending with a colon, the next line must be indented, and lines in the same block must line up exactly.
How to fix it
for i in range(3): print(i)
Output: 0 ⏎ 1 ⏎ 2
Tips
- Indent the body of
if,for,whileanddefby 4 spaces. - Lines in the same block must start at exactly the same column.
- Do not mix tabs and spaces; in Py-CRAFT, the Tab key inserts 4 spaces.
- "unexpected indent" means a line is indented when it should not be.