Coding Python
Home›Python Coding Games›Loop Factory

Loop Factory — for, while, break and continue

Each level names the exact lines that should come off the belt. Produce them with a loop.

Level 1

Checks
    Your PythonPython 3
    1

    How to play

    Every level states exactly what should be printed. Write a loop that produces it and press Run — the lines your program printed appear as items on a conveyor belt, each one marked against what was expected, so a wrong line is obvious at a glance rather than buried in a wall of text.

    Several levels ban the cheat. Level 3 forbids sum(), because the point is the accumulator pattern, not the answer.

    What each level teaches

    1. Count to five — for with range(), and that the stop value is excluded.
    2. Backwards by two — the third argument to range(), including negative steps.
    3. Add them all up — the accumulator pattern: start outside the loop, add inside it. This single shape underlies counting, totalling, filtering and building lists.
    4. Liftoff — while, and the responsibility that comes with it: you must change the condition yourself or the loop never ends.
    5. Stop at the first — break, which leaves the loop immediately and never looks at the remaining items.
    6. Build the triangle — nested loops, where the inner loop runs completely on every single turn of the outer one.

    for or while — which one?

    Use for when you know what you are looping over: a list, a string, a range of numbers. Use while when you are waiting for something to become true and cannot say in advance how many turns that will take. In practice for covers the large majority of loops in Python, partly because iterating directly over a list — for item in items — is almost always clearer than looping over indices.

    If your loop never stops

    It will be one of three things: the counter is never updated, it is updated in the wrong direction, or the condition can never become false. Everything here runs in a background worker with a timeout, so a runaway loop is stopped rather than freezing your browser — but the same mistake in a terminal needs Ctrl+C. The tutorial on Python loops covers continue, enumerate() and else on a loop, which this game leaves for later.

    Practise for, while, break and continue on your phone — the Coding Python app runs real Python 3 on your phone, with examples, quizzes and challenges built in.
    Get it free

    Frequently asked questions

    Why does range(1, 6) stop at 5?

    The stop value is always excluded. It looks odd at first but it makes range(len(items)) produce exactly the valid indices, and makes range(a, b) contain exactly b - a numbers.

    When should I use while instead of for?

    Use for when the number of turns is known in advance or you are iterating over a collection. Use while when you are repeating until some condition changes and cannot say how long that will take.

    What is the difference between break and continue?

    break leaves the loop entirely and nothing after it is examined. continue skips the rest of the current turn only and carries on with the next item.

    Why is sum() banned in level 3?

    Because the level is teaching the accumulator pattern — start at zero outside the loop, add inside it. You will reuse that shape constantly, including in places where no built-in exists to do it for you.

    Will an endless loop crash my browser?

    No. Your code runs in a background worker with a hard timeout, so a loop that never ends is stopped and you get a message rather than a frozen tab.

    Keep going

    ← All Python coding games  Read the tutorial →

    Take the games with you

    Coding Python packs a real Python 3 compiler, 30 example programs, 112 quiz questions and 12 self-checking challenges into one free Android app.

    Get it on Google Play
    Coding Python app icon