Skip main navigation

£199.99 £139.99 for one year of Unlimited learning. Offer ends on 28 February 2023 at 23:59 (UTC). T&Cs apply

Find out more

Python Turtle Race

In this step you will be writing a program using objects, using Python’s turtle module to create a turtle race.

In this step you will be writing a program using objects, using Python’s turtle module to create a turtle race.


animation of the Python turtle race program, 4 "turtles" are shown drawing lines across the screen at different speeds



1. Create a new Python program and save it as

2. You need to ask Python to import the Turtle class, which is like a blueprint for making a turtle. You will look at what a class is in more detail later on in the course; for now, use this code:


from turtle import Turtle


Tip: It is important you don’t name your file the same as an import, e.g., otherwise the code will try to import itself and will not work.



3. Create an instance of a Turtle object. I’m going to name my Turtle object ‘laura’. You can give your turtle whatever name you like.


laura = Turtle()


As the name of the Turtle object is a variable name, it must start with a letter and it cannot contain any spaces. You are creating a variable in exactly the same way as you usually do, except that the data type of the variable is not an integer or a string, but a Turtle!


Did you notice that a capital ‘T’ is used when referring to the Turtle class? This is because class names usually start with a capital so that they are easily distinguishable from variable names.


Each Turtle object is a different instance and will need a different name, so that when you give instructions, you can be specific about which object you are giving the instructions to.



4. Tell your Turtle object what it should look like. Inside the object are attributes, which are pieces of data we can define. The Turtle object has attributes for colour and shape; you can use the color and shape methods to customise those attributes:




You can also tell the Turtle object what to do by calling other methods.



5. Use the code below to instruct the turtle to stop drawing with penup(), then to move to a location with goto(), and finally to get ready to draw a line with pendown().


laura.goto(-160, 100)



6. Run your program. What happens?


The Python turtle window, 1 red turtle is displayed on the screen


Create three more instances of a Turtle object, each with a different name.


rik = Turtle()
lauren = Turtle()
carrieanne = Turtle()


If you send all the turtles to the same starting point, they will all be on top of each other.



7. Tell one new turtle to goto(-160, 70), one to goto(-160, 40), and one to goto(-160, 10), for example:


rik.goto(-160, 70)


You can also set a different colour for each turtle if you like, using the .color method as before.



8. Save and run your code to check that each of your turtles positions itself correctly, ready to start the race!


You now need to make the Turtle objects race. Each turtle will move forward by a random number of pixels.



9. After the code to create your four Turtle objects, add this code, replacing the names (laura, rik, etc.) with the names of your own turtle objects:


from random import randint

for movement in range(100):


Just as when you used methods to tell the turtle to penup(), pendown(), and goto(), with this code you are using the forward() method to ask it to move forward a random distance between one and five units.


Note: The randint function from the random module generates random integers.



10. Save and run your code and see which turtle wins! The result should be different each time you run the code.




Depending on your IDE, you might find that your code executes and then closes the window before you have had a chance to see the output. You can add this line to the end of your files to prevent this from happening:


input("Press Enter to close")


Experiment with the turtle race program, change some attributes and perhaps create some new turtle objects.

There is also a Code Club version of the ‘Turtle race’ project that you can try yourself, or if you are an educator, try with your learners.

This article is from the free online

Object-oriented Programming in Python: Create Your Own Adventure Game

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education