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
- 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.
- Scan the header for its terminating colon: for item in items: β the colon is not optional decoration.
- After adding it, confirm the following block is indented consistently, or an IndentationError follows next.
- Re-run: syntax errors are parse-time, so the fix is verified the moment the file loads.
Common wrong fixes
- Adding a semicolon instead of a colon out of C habit.
- Fixing the flagged line while the true omission sits on the previous header line.
- Concluding that the loop logic is wrong; before the colon exists, no logic has run at all.
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.
π 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
- Python debugging guide β a systematic method for finding bugs like this one fast.
- Browse every loops problem in this topic.
- Common Python errors and what they actually mean, plus the Python glossary for any term used on this page.