Turtle Challenge #205

Turtle Challenge 205 card

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

Here’s some Python turtle code. Before you run it, read it carefully. Can you guess what it will draw?

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

t = turtle.Turtle()
t.speed(0)
t.pensize(2)
colours = ["turquoise", "royalblue", "deeppink"]

for petal in range(8):
    t.pencolor(colours[petal % 3])
    t.circle(50)
    t.left(45)

t.hideturtle()
turtle.done()

Think about it

  1. How many circles will the turtle draw?
  2. Why does the turtle turn 45 degrees after each circle?
  3. What colours will the circles be?
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.