How to playEach puzzle shows a program with all indentation removed and the output it should print. Use the ◀ ▶ buttons (or Tab / Shift+Tab) to set each line's indentation, then press Check: the game runs your version with real Python and compares the output. Twelve puzzles go from a single if to nested loops, try/except and classes.
How to play
- Read the expected output. It tells you which lines must run inside a block and which run once.
- Indent the lines. Use ▶ to indent a line one level (4 spaces) and ◀ to outdent. On a keyboard: click a line, then Tab / Shift+Tab.
- Check. Your program runs with real Python. Wrong indentation shows the IndentationError or the wrong output.
- Clear all twelve. Progress is saved in this browser.
The indentation rules in one minute
- A line ending in a colon (
if,else,for,while,def,class,try,except) starts a block. - Every line in the block is indented one level more than the colon line — 4 spaces by convention.
- The block ends at the first line that is indented less. That line runs after the block, not inside it.
else,elifandexceptline up with theirifortry.
Run ▸for n in range(3):
print("inside the loop") # runs 3 times
print("after the loop") # runs once
The errors you'll meet
IndentationError: expected an indented block— a colon line with nothing indented under it.IndentationError: unexpected indent— a line indented for no reason.IndentationError: unindent does not match any outer indentation level— a line indented by an amount that doesn't line up with any block.
The trickiest bugs give no error: a print indented one level too far runs every time round the loop instead of once. That's why each puzzle checks the output, not just the syntax. Read more in Python syntax errors explained, or paste code into the syntax checker.
Frequently asked questions
Why does Python care about indentation?
Python uses indentation instead of braces to group statements into blocks. It makes code look the way it runs.
Should I use tabs or spaces?
Spaces — 4 per level, as recommended by PEP 8. Never mix tabs and spaces in one file.
Can there be more than one correct answer?
Sometimes. The game accepts any indentation that is valid Python and prints the expected output.
How do I fix IndentationError: unexpected indent?
Remove the extra spaces at the start of that line so it lines up with the lines around it.
Python Coding - Learn Python for Android: a real Python 3 interpreter, code editor, 26 example programs and 12 marked exercises.