Python turtle and tkinter

Spiral Party

Make the turtle spin rainbow spirals that grow and twist across a black sky.

Day 2 45 to 60 minutes 7 steps Key Stage 3
Start step 1

Yesterday every line was the same length. Today each line will be a little longer than the one before, so the turtle spirals outwards. Tiny changes to one number make wild new patterns, so keep experimenting.

You will learn

  • Use the loop counter i to make each line longer than the last
  • Store colours in a list and pick them in turn
  • Change the speed, the background and the title of the window
  • Write words in the turtle's window

You will need

  • A computer with Thonny or IDLE, or this page's Run it here button
  • Day 1's project helps, but you can start here

Words to know

variable
A name that stores a value. In the loop, i stores the number of the line.
list
Many values in one variable: ["red", "orange"]. The first item is number 0.
remainder
What is left over after dividing. i % 6 is the remainder of i divided by 6.
speed
How fast the turtle moves: 1 is slowest, 10 is fast and 0 is fastest of all.

Step by step

  1. Step 1: Start a new file

    Open Thonny, click New and save the file as spirals.py.

    Save the new file as spirals.py.
  2. Step 2: A square spiral

    In for i in range(100), the variable i counts 0, 1, 2 and so on up to 99, one number each time round the loop. So forward(i * 2) makes every line 2 steps longer than the one before.

    speed(0) is the turtle's fastest speed, so you do not have to wait.

    spirals.pyPython
    import turtle t = turtle.Turtle()t.speed(0) for i in range(100):    t.forward(i * 2)    t.left(90) turtle.done()
    Each line is a little longer, so the square turns into a spiral.
  3. Step 3: Give it a twist

    Change the turn from 90 degrees to 91. One degree more makes every corner miss the last one slightly, and the spiral twists.

    New: line 8.

    spirals.pyPython
    import turtle t = turtle.Turtle()t.speed(0) for i in range(100):    t.forward(i * 2)    t.left(91) turtle.done()
    91 degree turns: the square spiral twists round.
  4. Step 4: Turn on the party lights

    Make the background black with turtle.bgcolor("black"), and keep six colours in a list. i % 6 is the remainder when i is divided by 6, which goes 0, 1, 2, 3, 4, 5, 0, 1, 2... so colours[i % 6] picks the colours in turn, again and again.

    New: lines 3, 6 and 9.

    spirals.pyPython
    import turtle turtle.bgcolor("black")t = turtle.Turtle()t.speed(0)colours = ["red", "orange", "yellow", "green", "blue", "purple"] for i in range(100):    t.color(colours[i % 6])    t.forward(i * 2)    t.left(91) turtle.done()
    Each line takes the next colour from the list.
  5. Step 5: Make it a hexagon

    A hexagon turns 60 degrees at each corner, so 59 makes a twisting hexagon. Change the turn to 59 and the loop to 120 times. With six colours and six sides, each colour lines up on its own side.

    New: lines 8 and 11.

    spirals.pyPython
    import turtle turtle.bgcolor("black")t = turtle.Turtle()t.speed(0)colours = ["red", "orange", "yellow", "green", "blue", "purple"] for i in range(120):    t.color(colours[i % 6])    t.forward(i * 2)    t.left(59) turtle.done()
    Six colours, six sides: a rainbow hexagon spiral.
  6. Step 6: Thicker and thicker

    pensize can change inside the loop too. i / 30 + 1 starts at 1 and grows to 5 by the end, so the lines get thicker as the spiral grows.

    New: line 10.

    spirals.pyPython
    import turtle turtle.bgcolor("black")t = turtle.Turtle()t.speed(0)colours = ["red", "orange", "yellow", "green", "blue", "purple"] for i in range(120):    t.color(colours[i % 6])    t.pensize(i / 30 + 1)    t.forward(i * 2)    t.left(59) turtle.done()
    The outer lines are thicker than the inner ones.
  7. Step 7: Sign your work

    Give the window a title, hide the turtle, then lift the pen, go to the top of the window and write a message. align="center" puts the middle of the words at the turtle, and font sets the font, the size and the style.

    New: lines 4 and 15 to 20.

    spirals.pyPython
    import turtle turtle.bgcolor("black")turtle.title("Spiral Party")t = turtle.Turtle()t.speed(0)colours = ["red", "orange", "yellow", "green", "blue", "purple"] for i in range(120):    t.color(colours[i % 6])    t.pensize(i / 30 + 1)    t.forward(i * 2)    t.left(59) t.hideturtle()t.penup()t.goto(0, 260)t.color("white")t.write("Spiral Party!", align="center", font=("Arial", 28, "bold")) turtle.done()
    The finished Spiral Party, with its title.

Test it

Tick each one when it works. If one does not, look back at the step it comes from.

Stretch challenges

Finished early? Try these. Each one has a hint if you get stuck.

  1. Angle explorer

    Change 59 to other angles: try 89, 121, 144 and 175. Which makes your favourite pattern?

    Show a hint

    Angles just under 360 ÷ sides make that shape twist: 89 for squares, 119 for triangles, 71 for pentagons.

  2. Circle flower

    Draw 36 circles, turning 10 degrees after each one, to make a flower.

    Show a hint

    t.circle(100) draws a circle 100 steps across from the middle. 36 turns of 10 degrees make a full turn.

    Show one answer
    spirals.pyPython
    import turtle turtle.bgcolor("black")t = turtle.Turtle()t.speed(0)colours = ["red", "orange", "yellow", "green", "blue", "purple"] for i in range(36):    t.color(colours[i % 6])    t.circle(100)    t.left(10) turtle.done()
  3. Your choice

    Ask the user for the angle, then draw their spiral.

    Show a hint

    angle = int(input("Pick an angle: ")) gets a whole number. Then use t.left(angle).

    Show one answer
    spirals.pyPython
    import turtle angle = int(input("Pick an angle between 45 and 150: ")) turtle.bgcolor("black")t = turtle.Turtle()t.speed(0)colours = ["red", "orange", "yellow", "green", "blue", "purple"] for i in range(120):    t.color(colours[i % 6])    t.forward(i * 2)    t.left(angle) turtle.done()

The finished code

Show the whole program
spirals.pyPython
import turtle turtle.bgcolor("black")turtle.title("Spiral Party")t = turtle.Turtle()t.speed(0)colours = ["red", "orange", "yellow", "green", "blue", "purple"] for i in range(120):    t.color(colours[i % 6])    t.pensize(i / 30 + 1)    t.forward(i * 2)    t.left(59) t.hideturtle()t.penup()t.goto(0, 260)t.color("white")t.write("Spiral Party!", align="center", font=("Arial", 28, "bold")) turtle.done()
Notes for the teacher

Timing

  1. 5 minutes: a new file, and the square spiral.
  2. 25 minutes: steps 3 to 6.
  3. 10 minutes: step 7 and the tests.
  4. The rest: the angle explorer is a good whole class finish. Ask students to share their best angle.

Mistakes to look out for

  1. colours[i % 6] with a list of five colours: one colour is missed out, and with more than six the code crashes with IndexError: list index out of range. The 6 must match the number of colours in the list.
  2. turtle.bgcolor written as t.bgcolor: AttributeError. The background belongs to the screen, not the turtle.
  3. Writing the message before hiding the turtle leaves it sitting on the words: hide it first.

Going further

% (modulo) is worth a minute at the board: 7 % 6 is 1, 12 % 6 is 0. It is the same idea as a clock going round, and it appears again in the micro:bit and web tracks.