Bird
0
0

What will this code print?

medium📝 Predict Output Q5 of 15
Python - While Loop

What will this code print?

counter = 3
while counter > 0:
    print(counter)
    counter -= 1
A1 2 3
B3 2 1
C3 2 1 0
D0 1 2 3
Step-by-Step Solution
Solution:
  1. Step 1: Analyze loop condition and counter start

    Counter starts at 3 and loop runs while counter is greater than 0.
  2. Step 2: Trace loop output

    Prints 3, then 2, then 1; stops before printing 0 because 0 is not greater than 0.
  3. Final Answer:

    3 2 1 -> Option B
  4. Quick Check:

    Loop counts down from 3 to 1 [OK]
Quick Trick: Loop runs while counter > 0, decreasing each time [OK]
Common Mistakes:
MISTAKES
  • Including 0 in output
  • Counting up instead of down
  • Off-by-one errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes