×

python · level 1 · unit 5 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. 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.