YOUR SAVED CODE-ALONG
Code along: Python for else
Keep the lesson open on your other screen. Your edits stay in this workspace, separate from the practice activity. Sign in to save and reload a private account draft across devices, or share a snapshot when you need help.
CODE-ALONG / THEATER MODE
Understand for else
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.
Video settings & progress
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.
Start the video to enable typing controls.
YOUR TURN
Code along: Python for else
Pause Rob’s recording to type and run short examples. Compare normal exhaustion with break. Try a word with a vowel, a word without one and an empty word. Align else with for. These notes outline the topic; this open workspace has no fixed-answer checks and is separate from the scored practice.
Load an example, edit it and run it. Word wrap is enabled.
Your check results
Lesson reference
Examples load into this same code-along only after you confirm replacing your code.
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
See the attached lesson’s comic
