Decisions inside Python loops
An if indented inside a loop's block is checked again on every repetition, so each pass can make a different decision.
Understanding Decisions inside Python loops in Years 7–8 Creating Digital Solutions
What it does: The if is indented inside the for loop, so every n is tested and count only goes up when n is even. The if sits inside the while block, so each new guess gets its own check on every repetition. The variable being tested can change on each pass, so the condition can be True one time and False the next. The if is checked for n = 1, 2, 3 and 4, but it is only True when n is 3. So the message prints once. The if is checked on all 3 passes. n is only 1, 2 or 3, so n > 5 is False every time and print never runs.
Common mistakes: The else line needs a colon at the end. It should be 'else:' so that the indented block below it belongs to the else. range is a function, so it needs round brackets, not square ones. It should be 'for i in range(5):'.
Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.
This module's key terms are also in a printable crossword (Modules 50–125) and word search (Modules 3–125).
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
if inside a loopFill in the Blanks
Spot the Error
Student with a class code? Join your class →