Bird
0
0

What is wrong with this code?

medium📝 Debug Q7 of 15
Python - While Loop

What is wrong with this code?

counter = 0
while counter < 3:
    print(counter)
ALoop condition should be <= 3
BCounter should start at 1
CPrint statement is outside the loop
DCounter is never increased, causing an infinite loop
Step-by-Step Solution
Solution:
  1. Step 1: Check if counter changes inside loop

    Counter is not increased inside the loop, so it stays 0 forever.
  2. Step 2: Understand loop effect

    Without increment, loop condition is always true, causing infinite loop.
  3. Final Answer:

    Counter is never increased, causing an infinite loop -> Option D
  4. Quick Check:

    Always increment counter to avoid infinite loops [OK]
Quick Trick: Always update counter inside while loop [OK]
Common Mistakes:
MISTAKES
  • Forgetting to increment counter
  • Wrong loop condition
  • Misplacing print statement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes