Bird
0
0

Which of the following is the correct syntax for a while loop with an else block?

easy📝 Syntax Q3 of 15
Python - While Loop
Which of the following is the correct syntax for a while loop with an else block?
Aelse: while condition: # code
Bwhile condition: else: # code # code
Cwhile condition: # code else: # code
Dwhile condition: # code else: # code
Step-by-Step Solution
Solution:
  1. Step 1: Recall correct indentation

    The else block must be at the same indentation level as while, after the loop body.
  2. Step 2: Check each option

    while condition: # code else: # code shows correct syntax: while, indented code, then else at same level as while.
  3. Final Answer:

    while condition: # code else: # code -> Option D
  4. Quick Check:

    Else aligned with while, not inside loop [OK]
Quick Trick: Else must align with while, not inside loop body [OK]
Common Mistakes:
MISTAKES
  • Indenting else inside while body
  • Placing else before while
  • Missing colon after else

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes