Turtle Challenge #131

Turtle Challenge 131 card

Challenge type: Turtle Challenges  ·  Difficulty: ★★☆☆☆ Easy  ·  Language: Python

Beep is thinking hard about this code. What picture will the turtle make? Make a guess, then try it!

Turtle challenge 131: the code is shown below as text too.
Read the code. What will it draw?
import turtle

t = turtle.Turtle()
t.speed(0)
t.penup()

for row in range(6):
    for col in range(4):
        t.goto(col * 50, row * 50)
        t.dot(20, "gold" if row % 2 == 0 else "mediumpurple")

t.hideturtle()
turtle.done()

Think about it

  1. How many dots will there be?
  2. What pattern do the colours make?
  3. Why is there t.penup() at the start?
Need a hint?

Read the loop first. How many times does it repeat, and what does the turtle do each time? Draw it on paper one step at a time, turning your pencil like the turtle turns.

Grown-ups: This code runs in IDLE, the editor that comes with Python. Open a new file, paste the code, save it (any name except turtle.py) and press F5. A window opens and the turtle draws.

For teachers and grown-ups

Skills practised: Reading code, tracing loops, and connecting angles and shapes.

The big idea: Reading code and predicting what it does (tracing) is as important as writing it.

Questions to ask:

  • How many times does the loop repeat?
  • How far does the turtle turn altogether?
  • Which single number would you change to make a different shape?

Take it further: Children sketch their prediction first, run the code in IDLE, then change one value and predict again.