Concept
The Weird Loop else
Python lets you slap an `else` block directly onto a `for` or `while` loop:
python
for item in ["safe1", "safe2"]:
if item == "virus":
print("Virus found!")
break
else:
print("Zero threats found!")
# 62. The Weird Loop else Python lets you slap an `else` block directly onto a `for` or `while` loop: ```python for item in ["safe1", "safe2"]: if item == "virus": print("Virus found!") break else: print("Zero threats found!") ``` The loop's `else` block runs **only if the loop finished all turns naturally without hitting a `break`**.
Checkpoint
When does a loop's `else` block actually execute?
Practice
Write a `for` loop over `items = ["clean", "clean"]`. If `"dirty"` is found, `break`. Attach an `else:` block that prints `"All clean!"`.
01
Tap code blocks below to assemble
Available Blocks