Bird
0
0

Find the error in this code snippet:

medium📝 Debug Q14 of 15
Python - While Loop

Find the error in this code snippet:

i = 0
while i < 5:
    if i == 3:
        break
    print(i)
    i += 1
else:
print('Finished')
AThe else block is not indented properly.
BThe break statement is outside the loop.
CThe while condition is invalid.
DThe print statement inside the loop is missing parentheses.
Step-by-Step Solution
Solution:
  1. Step 1: Check indentation of else block

    The else block must be aligned with the while statement, but here it is not indented properly.
  2. Step 2: Verify other parts

    Break is inside the loop, while condition is valid, and print uses parentheses correctly.
  3. Final Answer:

    The else block is not indented properly. -> Option A
  4. Quick Check:

    Else must align with while, indentation error [OK]
Quick Trick: Else must align with while, check indentation [OK]
Common Mistakes:
MISTAKES
  • Misplacing else inside loop body
  • Confusing break placement
  • Ignoring indentation errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes