Recall & Review
beginner
What does the
else block do in a while–else statement?The
else block runs only if the while loop finishes normally without encountering a break statement.Click to reveal answer
beginner
When is the
else block skipped in a while–else loop?The
else block is skipped if the while loop exits early due to a break statement.Click to reveal answer
beginner
Consider this code:<br>
i = 0
while i < 3:
print(i)
i += 1
else:
print('Done')<br>What will be printed?The output will be:<br>0<br>1<br>2<br>Done<br>The
else runs because the loop finished normally.Click to reveal answer
beginner
What happens if a
break is used inside a while loop with an else?If
break runs, the loop stops immediately and the else block does not run.Click to reveal answer
intermediate
Why might you use a
while–else instead of just a while loop?You use
while–else to run special code only if the loop did not stop early, like confirming a search found nothing.Click to reveal answer
What triggers the
else block in a while–else loop?✗ Incorrect
The
else block runs only if the loop finishes normally without a break.If a
while loop never runs because the condition is false at start, does the else block run?✗ Incorrect
The
else block runs if the loop condition is false initially and the loop body never executes.What happens if a
break is inside the while loop?✗ Incorrect
A
break stops the loop early and skips the else block.Which of these is a good use of
while–else?✗ Incorrect
while–else is useful to run code if the loop completes without finding something.What will this code print?<br>
i = 0
while i < 5:
if i == 3:
break
print(i)
i += 1
else:
print('Finished')✗ Incorrect
The loop breaks at i == 3, so
else does not run. It prints 0,1,2 only.Explain how the
else block works with a while loop in Python.Think about when the loop ends normally versus early.
You got /3 concepts.
Describe a real-life example where a
while–else loop would be helpful.Imagine looking for your keys and what you do if you find or don't find them.
You got /3 concepts.