You want to print numbers from 1 to 10 but avoid infinite loops. Which code snippet correctly does this?
hard📝 Application Q8 of 15
Python - While Loop
You want to print numbers from 1 to 10 but avoid infinite loops. Which code snippet correctly does this?
Ai = 1
while i < 10:
print(i)
i -= 1
Bi = 1
while i <= 10:
print(i)
i += 1
Ci = 10
while i > 0:
print(i)
i += 1
Di = 1
while i <= 10:
print(i)
Step-by-Step Solution
Solution:
Step 1: Check loop condition and variable update
i = 1
while i <= 10:
print(i)
i += 1 increases i from 1 to 10, printing each number and stopping correctly.
Step 2: Verify other options
i = 1
while i < 10:
print(i)
i -= 1 decreases i but condition expects increase; i = 10
while i > 0:
print(i)
i += 1 increases i but condition expects decrease; i = 1
while i <= 10:
print(i) never updates i.
Final Answer:
i = 1
while i <= 10:
print(i)
i += 1 -> Option B
Quick Check:
Loop variable update matches exit condition [OK]
Quick Trick:Match variable update direction with loop condition [OK]
Common Mistakes:
MISTAKES
Updating variable in wrong direction
Forgetting to update variable
Using wrong loop condition
Master "While Loop" in Python
9 interactive learning modes - each teaches the same concept differently