Spelling bee
You say the word one letter at a time: c, a, t.
05LOOPS · LOOP OVER WORDS
A word is a row of letters. for can walk through it, one letter on each turn.
IN REAL LIFE
Every letter gets its own turn, from the first to the last.
You say the word one letter at a time: c, a, t.
Look for every A on a sign. You check each letter, one by one.
Raise one finger for each letter of your name. How many fingers?
LOOK FIRST
for toy in toys: walked through a list. for letter in word: walks through a word.
The box letter holds s, then u, then n.
A space is a letter too. It gets its own turn.
LOOK FIRST
Read each line and its note.
word = "sun"A text box with 3 letters.for letter in word:letter is a box. Each turn it holds the next letter: s, then u, then n. print(letter)Shows one letter on each turn.print("done!")Runs once, after the loop.RUN IT
Guess first. Then run real Python and check.
name = "PyBot"
for letter in name:
print(letter)How many lines will Python show?
PyBot's hint
Ready. Press Run Python.
Pyodide 314.0.3 runs CPython in your browser with WebAssembly.
Open-source under the Mozilla Public License 2.0. License
Now change one thing: Change PyBot to your name. Run again and count the lines.
YOUR TURN
Try each tiny question. A wrong answer becomes something to review.
PyBot spells dog out loud: d, o, g.
How many turns does that take?
for letter in "hi":
print(letter)What does Python show?
count = 0
for letter in "robot":
count = count + 1
print(count)What does Python show?
for letter in "a b":
print(letter)How many turns does the loop take?
count = 0
for letter in "robot":
if letter == "o":
count = count + 1
print(count)What does Python show?
new = ""
for letter in "abc":
new = letter + new
print(new)What does Python show?
for word in ["hi", "yo"]:
print(word)How many turns does the loop take?
PyBot wants to spell sun, one letter per line, but it shows the whole word 3 times. Fix the print line.
s u nOUTPUT
Want a fresh start? Clear only this page.
Next, you will put a loop inside another loop.