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.
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's a loop inside a loop here. How many squares do you think it will 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.
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?
Change one thing at a time, predicting each result before you run it. Then try a bigger grid with a fourth colour.
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.
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.
That's Python Foundations done. Python functions and events starts from a working program and builds it into something bigger.
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.
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.
Lesson done. You've finished every step you need.
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.