Answer key · 12 questions

Python while Loops Exercises With Answers

All 12 questions for Python while Loops in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

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.

01 · Multiple choice

What does a while loop check before each iteration?

  1. The indentation
  2. The print output
  3. The conditioncorrect
  4. The loop body

Answer: The condition

Why: If the condition is true, the body runs; otherwise the loop stops.

02 · Multiple choice

What does the program output?

n = 2
while n < 5:
    n += 1
print(n)
  1. 4
  2. 2
  3. 5correct
  4. 6

Answer: 5

Why: n increases from 2 until it reaches 5, then the condition fails.

03 · Multiple choice

Which change stops this loop from running forever?

n = 1
while n < 4:
    print(n)
  1. Add n += 1 inside the loopcorrect
  2. Remove the colon
  3. Use if instead of while
  4. Change print to return

Answer: Add n += 1 inside the loop

Why: The counter must change so the condition can become false.

04 · Multiple choice

What does break do inside a loop?

  1. Skips the current iteration
  2. Restarts the loop from zero
  3. Turns the condition into False
  4. Exits the loop immediatelycorrect

Answer: Exits the loop immediately

Why: break transfers control to the first statement after the loop.

05 · Fill in the blank

Complete the update so count decreases by one each time.

count = 3
while count > 0:
    print(count)
    count ____ 1

Answer: -=

Why: count -= 1 is shorthand for count = count - 1.

06 · Fill in the blank

Complete the condition so the loop prints 1 through 4.

n = 1
while n ____ 5:
    print(n)
    n += 1

Answer: <

Why: When n reaches 5, n < 5 is false and the loop stops.

07 · Fill in the blank

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.

08 · Fill in the blank

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.

09 · Predict output

What appears on the screen?

n = 0
while n < 3:
    n += 1
print(n)

Answer: 3

Why: The loop stops when n becomes 3.

10 · Predict output

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.

11 · Predict output

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.

12 · Predict output

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.

Practice this lesson