python · level 1 · unit 5 · lesson 3
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Put an if inside a loop to check every number.
Picture a postie walking down a street. At every door they ask the same question: does this house get a letter? Put an if inside a loop and Python can ask a question on every turn too.
The % sign gives the : what is left over after sharing into equal groups. Share 7 sweets between 2 friends and 1 is left over, so 7 % 2 is 1. Even numbers always leave 0.
To count matches, make a variable set to 0 before the loop. Inside the if, add 1 to it. Print it after the loop, once the counting is done.
for house in range(1, 7):
if house % 2 == 0:
print("Letter for house", house) The if is indented inside the loop, and the line under the if is indented once more.
guess first
Stomp or clap? Guess the rhythm.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
for beat in range(1, 5):
if beat % 2 == 0:
print("Clap")
else:
print("Stomp")
your turn
Your turn. Hand out free stickers.
Choose which visitors win a sticker by typing a number from 2 to 10. Type 5 and every 5th visitor wins. Run it to see how many of 30 visitors get one.
fix it
Even houses are on the left, so the postie should count 5. Fix it.
- The answer comes out as 1. Which line keeps wiping the count back to 0?
- left = 0 is inside the loop, so it runs at every house and throws the count away.
- Move left = 0 to the very top, above the for line, with no spaces in front.
nailed it
your loops can make choices now. :D
- An
ifinside a loop asks its question every turn. number % 2 == 0is True for even numbers.- Set the counter to 0 before the loop, not inside it.
new words in your collection
- remainder
This lesson isn’t saved. A free account remembers every lesson you finish.