← Back

Home β€Ί Problems β€Ί Missing Colon in for Loop

Missing Colon in for Loop

πŸ› Fix the bug Topic: loops Difficulty: easy Free

What this problem practises

Loops are where off-by-one bugs live. range() excludes its stop value, the loop variable keeps its last value even after the loop ends, and break versus continue changes the flow in ways that are easy to misread when you skim.

This is a fix-the-bug problem: a short program is given in its broken state, and the task is to repair the code so it produces the required output. The interactive editor runs your fixed version in the browser and checks it for you β€” no setup, no local interpreter.

Difficulty: easy β€” the fix or the trick is normally a single line, and a careful read should be enough.

The challenge

The code runs in PyDebug’s in-browser compiler. Open the problem, run the snippet, watch the exact error or output, and keep iterating until it is right. Signed-in solves count toward XP and your daily streak.

The error it raises

Run the broken snippet and Python stops with this exact error: SyntaxError: expected ':'. Match it to the Python errors guide, then repair the line that produced it β€” knowing the message is half the fix.

The snippet

The interactive editor pre-fills exactly this code, broken and ready to repair:

for i in range(5)
    print(i)

Understand the rule

Every compound statement header in Python β€” for, while, if, elif, else, def, class, try β€” must end with a colon; the colon plus indentation is how the language marks a block. Omitting it stops the parser immediately: SyntaxError points at (or just after) the header. Modern CPython even says 'expected ":"' outright.

How to trace it

  1. Trust the line number in the SyntaxError, but read one line above too β€” the parser sometimes notices a missing colon only when the next line confuses it.
  2. Scan the header for its terminating colon: for item in items: β€” the colon is not optional decoration.
  3. After adding it, confirm the following block is indented consistently, or an IndentationError follows next.
  4. Re-run: syntax errors are parse-time, so the fix is verified the moment the file loads.

Common wrong fixes

Rule to remember

Block headers always end with a colon. When a SyntaxError lands on or near a for/if/def line, check the colon first β€” it is the single most common cause.

Solve it in the interactive editor β†’

🐍 Learning path: Python Basics

Problem 4 of 12 in this path. Work them in order β€” each one builds on the rule before it.

← Inconsistent indentationNext in path: Missing colon after function definition β†’

More loops problems

Keep going