📱 Code Python on your phone with Python Coding - Learn PythonGet the free Android app →
  1. Home
  2. Python games
  3. Indent Fixer

Python game · Indentation, blocks, program flow

Indent Fixer: a Python indentation puzzle

Python uses indentation to know which lines belong inside an if, a loop or a function. These programs lost all their spaces. Indent each line so the program prints the expected output.

Solved 0/0

— make it print:

    
    

    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

    1. Read the expected output. It tells you which lines must run inside a block and which run once.
    2. 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.
    3. Check. Your program runs with real Python. Wrong indentation shows the IndentationError or the wrong output.
    4. Clear all twelve. Progress is saved in this browser.

    The indentation rules in one minute

    1. A line ending in a colon (if, else, for, while, def, class, try, except) starts a block.
    2. Every line in the block is indented one level more than the colon line — 4 spaces by convention.
    3. The block ends at the first line that is indented less. That line runs after the block, not inside it.
    4. else, elif and except line up with their if or try.
    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.

    Practise on your phone too

    Python Coding - Learn Python for Android: a real Python 3 interpreter, code editor, 26 example programs and 12 marked exercises.

    Get it on Google Play

    More games