Coding Python
Home›Python Coding Games›Code Scramble

Code Scramble

Eight Python programs with their lines shuffled. Drag them back into order.

Puzzle 1Moves 0

    What it printsPython 3

    Drag a line, or use the ▲▼ buttons. The program is right when every line is in a sensible place — variables defined before they are used, loop bodies under their loop, and the output printed at the end.

    How to read a scrambled program

    There is a method, and it works on every puzzle here:

    1. Find the setup. Lines that create a value — name = "Ada", counts = {}, total = 0 — almost always come first, because everything else depends on them.
    2. Find the headers. Any line ending in a colon (for …:, if …:, def …:) owns the indented lines beneath it. Put the header directly above its indented block.
    3. Follow the indentation. It is a map. A line indented by one level belongs inside the nearest header above it; two levels means it is inside a block inside a block.
    4. Put the result last. The print() that reports the answer usually comes after the loop that calculates it — if it is inside the loop, it prints every time round.

    That last point is the single most instructive thing in this game. Moving one print in or out of a loop changes the output completely, and seeing it happen is how the idea sticks.

    Why no typing makes it harder, not easier

    When you write a program yourself you can lean on habit — your fingers know that a for is followed by an indented line. Here you have to justify every placement, including the ones you would normally get right by accident. It is a reading exercise, and reading other people's code is most of what a working programmer actually does.

    After the puzzles

    Try writing the same programs from a blank editor: the FizzBuzz you just reassembled is lesson six's exercise, and the word counter is in the dictionaries lesson. If you can rebuild them from scratch, the ideas have landed.

    Prefer to code on your phone? Type these programs out yourself in the Coding Python app and watch them run on your phone.
    Get the free app

    Frequently asked questions

    What is Code Scramble?

    A Parsons problem: you are given all the lines of a correct Python program in the wrong order and you rearrange them. Because you never type, you can think purely about program flow — and it works well on a touchscreen.

    Do I have to fix the indentation too?

    No. Each line keeps the indentation it needs, so only the order is up to you. That keeps the puzzle about logic rather than whitespace.

    How do I move a line?

    Drag it with the mouse, or use the ▲▼ buttons on the right of each line. Keyboard users can focus a line and press Ctrl/Cmd with the arrow keys.

    Is the output real?

    Yes. Every puzzle's program is executed on CPython when the site is built, and the output you see on success is exactly what it printed.

    Why are these puzzles useful?

    Research on Parsons problems consistently finds they teach program structure faster than writing from scratch, because all the attention goes on order and control flow instead of syntax errors. They are also a good warm-up before writing the same program yourself.

    ← All Python coding games  Try Guess the Output →