Bird
0
0

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:
  1. 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.
  2. 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.
  3. Final Answer:

    i = 1 while i <= 10: print(i) i += 1 -> Option B
  4. 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes