Choosing a for loop or a while loop (Python)

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

Choosing a for loop or a while loop (Python)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. You want to print a message exactly 10 times. Which loop fits best?
A while True:
B for i in range(10):
2. A program keeps asking for a password until the user types the right one. Which loop fits best?
A for
B while
3. True or false: a for loop is a good choice when you know how many times to repeat.
A True
B False
4. Which task is best for a while loop?
A Keep asking for a word until the user types quit
B Print the numbers 1 to 5
5. Which loop is best for printing the 5 times table from 1 to 12?
A for n in range(1, 13):
B while answer != 'quit':

Match the Pairs

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

Left columnRight column
1for i in range(10): print(i) AA while loop: use it when repeating depends on a condition that can change, like until the user types quit
2while answer != "quit": answer = input("> ") BA while loop needs a condition that can change, not range(). For a fixed count of 10, use a for loop: for i in range(10):
3You know in advance that you want to repeat exactly 5 times, so choose a for loop Cfor turn in range(5):
4A while loop may run zero times if its condition is False the very first time it is checked DA for loop: use it when you know the loop must run a fixed number of times, such as 10
5while i in range(10): print(i) EA for loop cannot repeat until a condition changes. Use while answer != "quit": because we do not know how many times the user will type.
6for answer != "quit": answer = input("> ") Flives = 0 while lives > 0:

Definitions

for vs while

Fill in the Blanks

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

1.
___ i in range(10): print("Hello")
2.
command = input("Command: ") _____ command != "quit": command = input("Command: ")
3.
for i in range(_): print("Hi") print("Done")
4.
word = "" while word __ "quit": word = input("Type: ")
5.
# Show Go! 10 times for i in range(__): print("Go!")

Spot the Error

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

Question 1
A count = 0
B for count < 10:
C print(count)
D count += 1
Question 2
A text = ''
B while text == 'quit':
C text = input('Type: ')
D print('Bye')
Question 3
A answer = ''
B while answer != 'quit':
C input('> ')
D print('Goodbye')
Question 4
A n = 1
B while n <= 10:
C print(n)
D n -= 1
Question 5
A print('Lap counter')
B laps = 10
C for lap in laps:
D print(lap)

Answer Key

Choosing a for loop or a while loop (Python)
Section A: Quick Questions
Q1
B — for i in range(10):
Q2
B — while
Q3
A — True
Q4
A — Keep asking for a word until the user types quit
Q5
A — for n in range(1, 13):
Section B: Match the Pairs
Q1
for i in range(10): print(i) → A for loop: use it when you know the loop must run a fixed number of times, such as 10
Q2
while answer != "quit": answer = input("> ") → A while loop: use it when repeating depends on a condition that can change, like until the user types quit
Q3
You know in advance that you want to repeat exactly 5 times, so choose a for loop → for turn in range(5):
Q4
A while loop may run zero times if its condition is False the very first time it is checked → lives = 0 while lives > 0:
Q5
while i in range(10): print(i) → A while loop needs a condition that can change, not range(). For a fixed count of 10, use a for loop: for i in range(10):
Q6
for answer != "quit": answer = input("> ") → A for loop cannot repeat until a condition changes. Use while answer != "quit": because we do not know how many times the user will type.
Section C: Definitions
Q1
for vs while — Use for when you know how many times to repeat, like 10 times. Use while when repeating depends on a condition that can change, like until the user types quit.
Section D: Fill in the Blanks
Q1
for i in range(10): print("Hello")
Q2
command = input("Command: ") while command != "quit": command = input("Command: ")
Q3
for i in range(0): print("Hi") print("Done")
Q4
word = "" while word != "quit": word = input("Type: ")
Q5
# Show Go! 10 times for i in range(10): print("Go!")
Section E: Spot the Error
Q1
B — A for loop goes through a sequence such as range(10). It cannot take a condition. Either use 'while count < 10:' or write 'for count in range(10):'.
Q2
B — The loop should keep going until the user types quit, so the condition must be text != 'quit'. With == the condition is False at the start, so the loop never runs.
Q3
C — The typed value is thrown away, so answer never changes and the while condition stays True forever. The line should be 'answer = input('> ')'.
Q4
D — n goes down instead of up, so n <= 10 is always True and the loop never ends. A while loop must change its condition variable: n += 1.
Q5
C — laps is a single number, so a for loop cannot go through it and Python raises a TypeError. For a fixed count, use range(laps).