thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

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

All courses

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.

More options

Your lesson progress

Walkthrough: not startedPractice: not started

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.

Rob scans cat, finds the vowel a, breaks out of the loop and skips its else branch.
Rob’s comic strip · Loops

ORIGINAL WALKTHROUGH · 8:13

Watch with Rob.

Pause whenever you want to try a change. You can return to the lesson at any time.

LEARN WITH THE CODE ZONE

Unlock this video walkthrough

Written lessons and coding practice are free. Video walkthroughs are included with a paid Code Zone membership or an active free trial.

Have a Code Zone website account? Use your learning account’s email to get a six-digit sign-in code, or use your existing website password.

Need a website account? Paid Skool members and active trial members can use the email from their membership questions to request a sign-in code. No invite link or Skool password needed.

Close this window to keep practicing. After signing in, you’ll return to this lesson.

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)

Starter source code for VS Code

Copy the source below or download the file to follow along in VS Code.

The download includes a lesson link in each file and a README so you can find your way back.

main.py
main.pypython · example
# Code along with Rob: Python for else
# Compare normal exhaustion with break. Try a word with a vowel, a word without one and an empty word. Align else with for.
# Predict the output and keep experiments small.

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.

Finish the iteration before running else
loops.pypython · example
for number in range(1, 3):
    print(number)
else:
    print("Completed")
Open workspace to paste this example (opens in a new tab)

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.

Stop at the first match
loops.pypython · example
word = "cat"
for char in word:
    if char in "aeiou":
        print(f"Found: {char}")
        break
else:
    print("No vowels")
Open workspace to paste this example (opens in a new tab)

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.

Zero turns can still reach else
loops.pypython · example
for number in range(0):
    print("Never visited")
else:
    print("Completed")
Open workspace to paste this example (opens in a new tab)

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.

Keep break and continue distinct
loops.pypython · example
for number in range(3):
    if number == 1:
        continue
    print(number)
else:
    print("Completed")
Open workspace to paste this example (opens in a new tab)

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.

Expected output
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.

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.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

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)

Continue learning

ONE STEP FORWARD

Normal exhaustion reaches else; break skips it. Next, stop a vowel search at its first match.