← 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 end of Course A. The program below uses everything you have learned: loops, names, the counter, a list and the remainder. Read it first, then make it your own.

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 is a loop inside a loop. Read the code. How many squares will it 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.

Predict what a longer list does to the pattern.

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?

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.

    You have finished Course A. Course B starts from a working program and builds something bigger.

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

    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.

      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

      Now you can
      You reached the endBack to Python Foundations with Turtle