← Python Foundations with Turtle

Challenge: a tile

Read a tile pattern, change one thing and predict the result, then make a tile of your own.

Floors, fabrics, wrapping paper and websites are made from a tile repeated. Today you make the tile.

This is the last lesson of Python Foundations, and the program below uses everything you've learned: loops, names, the counter, a list and the remainder. Read it first, then make it your own.

How this lesson works: Predict, Run, Investigate, Modify, Make
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

tile.py
from turtle import *

colours = ["gold", "navy", "teal"]
size = 40
speed(0)

penup()
goto(-80, -60)
for row in range(3):
    for col in range(4):
        pick = (row + col) % len(colours)
        fillcolor(colours[pick])
        begin_fill()
        for side in range(4):
            forward(size)
            right(90)
        end_fill()
        forward(size)
    backward(size * 4)
    left(90)
    forward(size)
    right(90)

There's a loop inside a loop here. How many squares do you think it will draw?

How many squares will the program draw?

RunRun it and compare the result with your prediction.

Run

Run it and compare it with your prediction. Which line makes the colours repeat in a pattern?

Line What it does Why
for row in range(3): Makes 3 rows The outer loop
for col in range(4): Makes 4 squares in each row The inner loop runs fully for every row
pick = (row + col) % len(colours) Works out which colour to use The remainder goes 0, 1, 2, 0, 1, 2 ... however long the list is
fillcolor(colours[pick]) Uses that colour A list item picked by its place
tile.py
from turtle import *

colours = ["gold", "navy", "teal"]
size = 40
speed(0)

penup()
goto(-80, -60)
for row in range(3):
    for col in range(4):
        pick = (row + col) % len(colours)
        fillcolor(colours[pick])
        begin_fill()
        for side in range(4):
            forward(size)
            right(90)
        end_fill()
        forward(size)
    backward(size * 4)
    left(90)
    forward(size)
    right(90)
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

tile.py
from turtle import *

colours = ["gold", "navy", "teal"]
size = 40
speed(0)

penup()
goto(-80, -60)
for row in range(3):
    for col in range(4):
        pick = (row + col) % len(colours)
        fillcolor(colours[pick])
        begin_fill()
        for side in range(4):
            forward(size)
            right(90)
        end_fill()
        forward(size)
    backward(size * 4)
    left(90)
    forward(size)
    right(90)

Work out which colour one square gets.

What happens to the pattern when the list gets longer?

In row 0, column 1, what is pick?

colours has 4 colours instead of 3. What is pick in row 2, column 3?

ModifyChange the code on purpose and check the result.

Modify

Change one thing at a time, predicting each result before you run it. Then try a bigger grid with a fourth colour.

Try it yourselfYour edits are saved in this browser.
Need a hint?

Before a hint: what kind of stuck is this? Try a strategy first

Add a colour to the list, make the grid 4 rows by 5 columns, and move the start further left and down.

Another hint

backward(size * 5) must match the number of columns, so the next row starts at the left again.

Change the tile: a bigger grid and a fourth colour.

    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

    Make a tile of your own. Plan it on paper first, then build it:

    When it works, change one thing and predict what will happen before you run it.

    That's Python Foundations done. Python functions and events starts from a working program and builds it into something bigger.

    Try it yourselfYour edits are saved in this browser.
    Need a hint?

    Before a hint: what kind of stuck is this? Try a strategy first

    Start from the program above. Change the pattern rule: a decision instead of pick, or a different remainder.

    Another hint

    A checkerboard: if (row + col) % 2 == 0 picks one colour, else picks another.

    Make your own tile with a name for the size, a list of colours, a loop in a loop and a decision.

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

      Now you can
      Finished the course?Go over it: Python Foundations with Turtle recapEvery idea in one place, to check you've got it.You reached the endBack to Python Foundations with Turtle
      For teachers

      Students read a tile pattern that combines loops, names, a list and the remainder, change it, and make a tile of their own. About 40 minutes.

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

      Students will be able to

      Before class

      Run the tile, then make a checkerboard yourself. The tricky part is backward(size * 4) at the end of a row: it must match the number of columns.

      Running the lesson

      1. Predict and Run: the count is rows times columns. Ask students to say which loop is which.
      2. Investigate: students work out the colour of one square on paper. The remainder is what makes the pattern repeat.
      3. Modify: insist on a prediction before each run. Ask students to say what they expect, then check.
      4. Make: let students plan on paper first. Early finishers add a second decision, or a list of sizes.

      The hard part (on purpose)

      Planning the pattern before coding. Ask them to colour a small grid on paper and say the rule in words first.

      When students get stuck

      Stretch and support

      Stretch: Make the tile grow: each square a little bigger than the one before, using an update.

      Support: Give the working tile and ask the student to change only the list of colours, then only the size.

      Where it leads

      Course B starts from a working program and builds up to a game: functions, modules and events. Class ideas: print the tiles and arrange them in a class wall.

      What each step is for