Decisions inside Python loops

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Decisions inside Python loops

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. for n in range(5): if n > 2: print(n) How many times is the condition n > 2 checked?
A 5 times, once on every repetition
B Only once, before the loop starts
2. Which code checks every number from 1 to 10 and prints only the even ones?
A if n % 2 == 0: for n in range(1, 11): print(n)
B for n in range(1, 11): if n % 2 == 0: print(n)
3. True or false: an if inside a loop can make a different decision on each repetition.
A False
B True
4. Which line of this code makes the if part of the loop's block? for score in scores: if score >= 50: print("Pass")
A Indenting the if line under the for line
B Writing the if line with no indent
5. What does this loop print? for n in range(1, 5): if n == 3: print("Three!")
A Three! (printed once)
B Three! (printed 4 times)

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1for n in range(1, 6): if n > 3: print(n) AThe if is not indented, so it is outside the loop. It runs only once after the loop ends, and only checks the last value of n.
2while lives > 0: if hit: lives = lives - 1 Bfor n in range(1, 11): if n % 2 == 0: count = count + 1
3Count only the even numbers by testing each number in a for loop CThe if is not indented under the for line, so Python does not see it as part of the loop block, which causes an error.
4Keep asking in a while loop and make a decision each time the user answers Dwhile guess != 7: guess = int(input()) if guess < 7: print("Too low")
5for n in range(1, 6): print(n) if n == 3: print("Three!") EThe if is checked on every pass of the for loop, so only the numbers bigger than 3 are printed.
6for score in scores: if score > 50: print("Pass") FThe if is checked every time the while loop repeats, and lives only goes down on passes where hit is True.

Definitions

if inside a loop

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
for n in range(1, 6): __ n > 3: print(n)
2.
for score in [4, 9, 7]: if score _ 5: print("Pass")
3.
for n in range(1, 11): if n % 2 == 0_ print(n)
4.
n = 1 _____ n <= 5: if n == 3: print("Three!") n = n + 1
5.
for age in [8, 12, 14]: if age >= 12: ____print("Teen")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A for n in range(1, 6):
B if n % 2 == 0:
C print(n, "is even")
D else
E print(n, "is odd")
Question 2
A for i in range[5]:
B if i == 3:
C print("Three!")
Question 3
A count = 0
B while count > 5:
C if count == 2:
D print("Two!")
E count = count + 1
Question 4
A total = 0
B for n in range(1, 11):
C if n > 5:
D total == total + n
E print(total)
Question 5
A for score in [40, 75, 90]:
B if score => 50:
C print("Pass")
D else:
E print("Try again")

Answer Key

Decisions inside Python loops
Section A: Quick Questions
Q1
A — 5 times, once on every repetition
Q2
B — for n in range(1, 11): if n % 2 == 0: print(n)
Q3
B — True
Q4
A — Indenting the if line under the for line
Q5
A — Three! (printed once)
Section B: Match the Pairs
Q1
for n in range(1, 6): if n > 3: print(n) → The if is checked on every pass of the for loop, so only the numbers bigger than 3 are printed.
Q2
while lives > 0: if hit: lives = lives - 1 → The if is checked every time the while loop repeats, and lives only goes down on passes where hit is True.
Q3
Count only the even numbers by testing each number in a for loop → for n in range(1, 11): if n % 2 == 0: count = count + 1
Q4
Keep asking in a while loop and make a decision each time the user answers → while guess != 7: guess = int(input()) if guess < 7: print("Too low")
Q5
for n in range(1, 6): print(n) if n == 3: print("Three!") → The if is not indented, so it is outside the loop. It runs only once after the loop ends, and only checks the last value of n.
Q6
for score in scores: if score > 50: print("Pass") → The if is not indented under the for line, so Python does not see it as part of the loop block, which causes an error.
Section C: Definitions
Q1
if inside a loop — An if indented inside a loop's block is checked again on every repetition, so each pass can make a different decision.
Section D: Fill in the Blanks
Q1
for n in range(1, 6): if n > 3: print(n)
Q2
for score in [4, 9, 7]: if score > 5: print("Pass")
Q3
for n in range(1, 11): if n % 2 == 0: print(n)
Q4
n = 1 while n <= 5: if n == 3: print("Three!") n = n + 1
Q5
for age in [8, 12, 14]: if age >= 12: print("Teen")
Section E: Spot the Error
Q1
D — The else line needs a colon at the end. It should be 'else:' so that the indented block below it belongs to the else.
Q2
A — range is a function, so it needs round brackets, not square ones. It should be 'for i in range(5):'.
Q3
B — count starts at 0, so 'count > 5' is False straight away and the loop (and the if inside it) never runs. It should be 'while count < 5:'.
Q4
D — A double == compares values and does not store anything. Use a single = to update the total: 'total = total + n'.
Q5
B — '=>' is not a Python comparison operator. Greater than or equal to is written '>=', so it should be 'if score >= 50:'.