This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python while Loops, with the exact answer and a short explanation for every question.
The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.
What does a while loop check before each iteration?
Answer: The condition
Why: If the condition is true, the body runs; otherwise the loop stops.
What does the program output?
n = 2
while n < 5:
n += 1
print(n)Answer: 5
Why: n increases from 2 until it reaches 5, then the condition fails.
Which change stops this loop from running forever?
n = 1
while n < 4:
print(n)Answer: Add n += 1 inside the loop
Why: The counter must change so the condition can become false.
What does break do inside a loop?
Answer: Exits the loop immediately
Why: break transfers control to the first statement after the loop.
Complete the update so count decreases by one each time.
count = 3
while count > 0:
print(count)
count ____ 1Answer: -=
Why: count -= 1 is shorthand for count = count - 1.
Complete the condition so the loop prints 1 through 4.
n = 1
while n ____ 5:
print(n)
n += 1Answer: <
Why: When n reaches 5, n < 5 is false and the loop stops.
Complete the keyword that leaves the loop immediately.
while True:
n = 1
if n == 1:
____Answer: break
Why: break ends the loop even when the condition is still true.
Complete the keyword that skips the rest of this iteration.
n = 0
while n < 3:
n += 1
if n == 2:
____
print(n)Answer: continue
Why: continue jumps back to the condition without running the rest of the body.
What appears on the screen?
n = 0
while n < 3:
n += 1
print(n)Answer: 3
Why: The loop stops when n becomes 3.
What is printed?
total = 0
n = 1
while n <= 4:
total += n
n += 1
print(total)Answer: 10
Why: The loop adds 1 + 2 + 3 + 4.
What is the output?
n = 0
while True:
n += 1
if n == 3:
break
print(n)Answer: 3
Why: break runs when n reaches 3, then print shows it.
What does this code print?
n = 5
while n > 0:
n -= 2
print(n)Answer: -1
Why: The sequence is 3, 1, -1; the loop stops when n is no longer positive.
Try the questions first
Open the practice page for Python while Loops and answer the same 12 questions with instant feedback.