03.03 · Loops / Watch · 16 min
Tell a completed search from an early stop
A loop’s else belongs to the loop. It does not mean the condition inside the loop was false.
Activity 3 of 8 in Loops
Python 2: Logic, Loops & Functions / 03.03
Understand for else
Not started
Use break to stop a search and distinguish normal completion from the loop else.
Your lesson progress
Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

ORIGINAL WALKTHROUGH · 8:13
Watch with Rob.
Pause whenever you want to try a change. You can return to the lesson at any time.
You are signed in, but video access needs attention. Check your account access. Your saved work is kept.
Video walkthroughs are included with an active Code Zone membership. The written lesson and practice are free.
Open video & code editor (opens in a new tab)A loop else runs after normal exhaustion
Align else with for, not with an if inside its body. When the iterable is exhausted normally, the else block runs. Here the loop prints 1 and 2 and then the completion message.
This is different from an if/else decision. The loop may have run many times before its else runs. An error is not normal completion.
for number in range(1, 3):
print(number)
else:
print("Completed")break exits this loop and skips its else
The loop checks each character in cat. c is not a vowel, but a is. The program prints Found: a, then break exits the loop. It never visits t, and the loop’s else does not run.
The comic traces these same characters. A missing break would continue the search and eventually run else, even after finding a vowel.
word = "cat"
for char in word:
if char in "aeiou":
print(f"Found: {char}")
break
else:
print("No vowels")An empty iteration still completes normally
range(0) supplies no values. The body never runs, but the loop has still exhausted its iterable without break, so Completed prints.
In a search, else is useful for the no-match result. The next practice uses the source vowel-search task and a word containing more than one vowel.
for number in range(0):
print("Never visited")
else:
print("Completed")continue skips a turn, not the loop else
continue skips the rest of the current body and moves to the next iteration. It does not terminate the loop like break. This example skips printing 1, but after reaching the end it still prints Completed.
Use break for the first-match search in the practice. Use continue only when you want another turn.
for number in range(3):
if number == 1:
continue
print(number)
else:
print("Completed")Build it one step at a time
Prefer learning by typing? Work through short instructions, run the Python code and check each change before continuing. Your guided project saves separately from the challenge below.
The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.
Start mini-lesson(opens in a new tab)3 steps · Saved work · Instant feedback
YOUR TURN
Stop at the first matching vowel
Keep word = "team" and vowels = "aeiou". Stop the search immediately after printing the first found message. Preserve the loop else for words with no matching lowercase vowel.
Found a vowel!
Keep the starting inputs. Print these lines in this order, without extra output.
- Keep word as team
- Keep the lowercase vowel set
- Stop with char at the first vowel e
Edit Python, then run it. Check challenge checks your output and the listed Python requirements. Your code saves on this browser after a short pause. Sign in and choose Save account draft to keep it across devices. Word wrap is enabled.
Ready to checkRun the checks to see your results here.
Your check results
Need a nudge?
Finding a match should exit the search
break exits the loop and skips its else. Put it in the same if block as the found message, after that print.
One possible solution
Your code may look different and still be correct. Compare the approach with your own.
break stops on e and skips both the later vowel a and the no-match else. A word such as rhythm exhausts normally and reaches else.
word = "team"
vowels = "aeiou"
for char in word:
if char in vowels:
print("Found a vowel!")
break
else:
print("No vowels found in the word.")Ready to try it?
Practice in your browser and see your results.
Progress details
Have a question?
Ask the Code Zone community.
Ask a question in The Code Zone (opens in a new tab)