← Python Foundations with Turtle

A street of houses

Read a whole program that uses loops and variables together, then change it and add to it.

Real programs are longer than a few lines. Programmers spend more time reading code than writing it: they find the part that does a job, then change just that part.

This program uses only what you already know: loops, names, the counter and updates. It is longer, so the skill now is reading: finding which part does which job, and how the parts depend on each other.

Info

Watch it built

Watch the program being built, one part at a time. Each caption names the job of the lines that follow it. You will need those jobs in a moment.

Watch it built
PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

street.py
from turtle import *

speed(0)
houses = 4
width = 60
gap = 20

penup()
backward(220)
pendown()

for house in range(houses):
    # walls
    for side in range(4):
        forward(width)
        left(90)
    # roof
    left(90)
    forward(width)
    right(30)
    forward(width)
    right(120)
    forward(width)
    right(30)
    forward(width)
    left(90)
    # move along to the next house
    penup()
    forward(gap)
    pendown()

print(houses * width + (houses - 1) * gap)

You saw it print 300 with 4 houses. Read the last line again: what changes when there is one more house?

Change houses to 5. What number will the last line print?

RunRun it and compare the result with your prediction.

Run

Change houses to 5, run it and compare with your prediction. Watch the turtle: which part of the program is it running right now?

street.py
from turtle import *

speed(0)
houses = 4
width = 60
gap = 20

penup()
backward(220)
pendown()

for house in range(houses):
    # walls
    for side in range(4):
        forward(width)
        left(90)
    # roof
    left(90)
    forward(width)
    right(30)
    forward(width)
    right(120)
    forward(width)
    right(30)
    forward(width)
    left(90)
    # move along to the next house
    penup()
    forward(gap)
    pendown()

print(houses * width + (houses - 1) * gap)
Try it yourselfYour edits are saved in this browser.

InvestigateTrace how each line of code connects to what you see.

Investigate

street.py
from turtle import *

speed(0)
houses = 4
width = 60
gap = 20

penup()
backward(220)
pendown()

for house in range(houses):
    # walls
    for side in range(4):
        forward(width)
        left(90)
    # roof
    left(90)
    forward(width)
    right(30)
    forward(width)
    right(120)
    forward(width)
    right(30)
    forward(width)
    left(90)
    # move along to the next house
    penup()
    forward(gap)
    pendown()

print(houses * width + (houses - 1) * gap)

Match each part with its job.

What depends on width?

This version looks fine at first. Change a number and it isn't.

Match each part of the program with its job.

for side in range(4):
left(90) then forward(width), right(30) ...
penup(), forward(gap), pendown()
for house in range(houses):

Back to 4 houses. Change width to 70 and leave the rest. What will the last line print?

With houses = 4 this street looks right. Change houses to 6 and the roofs go wild. Click the wrong line.

ModifyChange the code on purpose and check the result.

Modify

Change the street one step at a time. Run after each change.

Fit six houses on the screen.

Need a hint?

The screen's right edge is about x = 250. The printed number is the street's length, starting from x = -220.

Another hint

Six houses need six widths and five gaps. Try a smaller width.

Make each house a little wider than the last.

Need a hint?

Which line runs once per house? A change there happens to every house.

Another hint

Look back at size = size + 5 in A value that grows. Add a line at the end of the house loop.

The last line works out the length from width. Is it still true?

Try it yourselfYour edits are saved in this browser.

Draw 6 houses that all fit on the screen.

    Run your program. This ticks when your code does all of the above.

    Make each house 10 wider than the one before.

      Run your program. This ticks when your code does all of the above.

      MakeBuild something new using what you have learned. Expect a few tries; that is normal.

      Make

      Add something new after the houses: a fence, a row of trees or a shop.

      Give it its own loop, after the house loop.

      Need a hint?

      Decide what one part looks like (one fence post, one tree), then what repeats.

      Another hint

      One post: turn up, go up, come back down, turn back, step along. Put those under a for line.

      Give it its own variable, so how many or how big is set in one place.

      Try it yourselfYour edits are saved in this browser.

      Add something to the street that is drawn by its own loop.

        Run your program. This ticks when your code does all of the above.

        ModifyChange the code on purpose and check the result.

        Lay a tiled floor

        Tilers plan floors like this: one tile, repeated. This program is a loop inside a loop, like the street. Which loop draws one tile, and which repeats the tile? Change the number of tiles, make them bigger, then add a second row.

        Try it yourselfYour edits are saved in this browser.
        For teachers

        Students watch a 30-line program being built part by part, then read it closely, change it and add to it. It uses only loops, variables and calculations from lessons 1 and 2; the new skill is reading a whole program. About 45 minutes.

        Open the teacher guide: answers, solutions, slides and a worksheet.

        Students will be able to

        Before class

        Watch the build clip, then try the bug task with houses = 6. The bug (a loop that repeats houses times instead of 4) hides while houses is 4; that is the point of the lesson: parts of a program depend on each other in ways you only see when something changes.

        Running the lesson

        1. Watch: play the clip once, whole class. Pause after each caption and ask what the next lines will do before they appear. The captions are the program's parts: name the numbers, move to the start, one house (walls, roof, step along), work out the length.
        2. Predict: students commit to the printed length for 5 houses before running. The common wrong answer is 400 (5 gaps instead of 4): ask them to count the gaps on the 4-house picture.
        3. Investigate: this is the heart of the lesson. The match and trace tasks are about parts and how they connect, not single lines. Ask: which lines would you change to make the roofs taller? (None alone: the roof is drawn with width, so it follows the walls.)
        4. Modify: use the live slide to change one number together (houses to 6 shows the length growing), then students work on their own screens. They change one thing and run. The third task is a real question: after the houses grow, the printed length is wrong, because the formula assumes every house is the same width.
        5. Make: students work alone here; walk round, or use the live slide to build one part with the whole class. The new part needs its own loop and variable. Early finishers can make it depend on an existing variable (a fence as long as the street).

        The hard part (on purpose)

        Reading a program longer than one screen. Ask students to say the job of each part aloud before they look at any single line.

        When students get stuck

        Stretch and support

        Stretch: Make the fence exactly as long as the street, using the same calculation as the last line.

        Support: Give the match task first on paper, cutting the program into its parts, before students touch the code.

        Where it leads

        This is the shape of every later unit: a few short lessons on new ideas, then a longer program that puts them together. Next unit adds decisions (if) and lists, and its programs grow the same way.

        What each step is for

        Now you can
        Next lesson7. If and elseMake a program choose which lines to run, using if and else.