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.
Make your prediction first. Help opens once you've locked it in.
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?
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 |
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)
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?
Change one thing at a time, predicting each result before you run it. Then try a bigger grid with a fourth colour.
Add a colour to the list, make the grid 4 rows by 5 columns, and move the start further left and down.
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.
Make a tile of your own. Plan it on paper first, then build it:
if / else) that picks somethingWhen 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.
Start from the program above. Change the pattern rule: a decision instead of pick, or a different remainder.
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.
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.
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.
Planning the pattern before coding. Ask them to colour a small grid on paper and say the rule in words first.
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.
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.