Programming lesson
Simulating the Tortoise vs. Hare Race with Python Turtle
Learn to code a classic race simulation using Python's turtle module, random number generation, and while loops. Perfect for understanding probability, functions, and graphical output.
Introduction: Why Simulate the Tortoise vs. Hare?
In 2026, the legendary fable gets a digital twist. Whether you're into retro gaming, AI simulations, or just need a fun Python project, this tutorial teaches you to build a graphical race using the turtle module. You'll master random number generation, function design, and while loops—all while watching a tortoise and hare compete pixel by pixel.
Setting Up the Race Course
First, import the turtle module and create the window. Use turtle.Screen() to set up a 400x200 canvas. The starting line is at x = -100, the finish line at x = 100. Each animal stays in its own lane: tortoise at y=0, hare at y=50.
import turtle
import random
wn = turtle.Screen()
wn.title("Tortoise vs. Hare - Python Edition")
wn.setup(width=400, height=200)
wn.bgcolor("lightgreen")Create helper turtles to draw lines and write messages. Use hideturtle() so they don't interfere with the racers.
# Draw start and finish lines
drawer = turtle.Turtle()
drawer.hideturtle()
drawer.penup()
drawer.goto(-100, -30)
drawer.pendown()
drawer.goto(-100, 80)
drawer.penup()
drawer.goto(100, -30)
drawer.pendown()
drawer.goto(100, 80)
drawer.penup()
drawer.goto(-100, -40)
drawer.write("Start", align="center")
drawer.goto(100, -40)
drawer.write("Finish", align="center")Creating the Animal Functions
Each animal has a move function that takes its current x-position and returns the new position. Use random.randint(1,10) to pick a move based on probabilities.
Tortoise Movement Function
def tortoise_move(pos):
num = random.randint(1,10)
if num <= 5: # Fast Plod (50%)
pos += 3
elif num <= 7: # Slip (20%)
pos -= 5
else: # Slow Plod (30%)
pos += 1
# Boundary check
if pos < -100:
pos = -100
elif pos > 100:
pos = 100
return posHare Movement Function
def hare_move(pos):
num = random.randint(1,10)
if num <= 2: # Sleep (20%)
pass
elif num <= 4: # Big Hop (20%)
pos += 7
elif num <= 5: # Big Slip (10%)
pos -= 10
elif num <= 8: # Small Hop (30%)
pos += 1
else: # Small Slip (20%)
pos -= 2
if pos < -100:
pos = -100
elif pos > 100:
pos = 100
return posSetting Up the Racers
Create two turtle objects for the animals. Use shape() to set their cursors and shapesize() to make them visible.
tortoise = turtle.Turtle()
tortoise.shape("turtle")
tortoise.color("green")
tortoise.shapesize(1.5, 1.5, 1)
tortoise.penup()
tortoise.goto(-100, 0)
hare = turtle.Turtle()
hare.shape("classic")
hare.color("red")
hare.shapesize(1.5, 1.5, 1)
hare.penup()
hare.goto(-100, 50)Running the Race with a While Loop
Use a while loop that continues until either animal reaches x=100. Increment a clock variable each iteration.
clock = 0
tortoise_x = -100
hare_x = -100
while tortoise_x < 100 and hare_x < 100:
clock += 1
tortoise_x = tortoise_move(tortoise_x)
hare_x = hare_move(hare_x)
tortoise.goto(tortoise_x, 0)
hare.goto(hare_x, 50)Notice the loop condition: both must be below 100. If one reaches 100, the loop ends. But we need to update positions after the move, so the final position may be exactly 100.
Determining the Winner
After the loop, compare final x-positions. Tortoise wins ties.
if tortoise_x >= hare_x:
winner = "Tortoise"
wn.title("Tortoise Wins!")
else:
winner = "Hare"
wn.title("Hare Wins!")
print(f"{winner} wins in {clock} seconds!")
# Display message on screen
drawer.goto(0, -60)
drawer.write(f"{winner} wins in {clock} ticks!", align="center", font=("Arial", 12, "normal"))Full Code and Testing
Combine all parts. Run the simulation multiple times. Notice how the tortoise often wins despite moving slower on average—just like in the fable! This is due to the hare's sleep and slips.
Example output: "Tortoise wins in 45 seconds!" or "Hare wins in 22 seconds!"
Extensions and Real-World Connections
This simulation mirrors AI pathfinding and game physics. In 2026, similar random-walk models are used in machine learning for exploration vs. exploitation. You can extend this project:
- Add sound effects when an animal slips.
- Implement a betting system using Python dictionaries.
- Create a leaderboard for multiple races using file I/O.
Common Pitfalls and Tips
- Boundaries: Always reset positions to -100 or 100. Otherwise, animals may go off-screen.
- Randomness: Use
random.seed()for reproducible results during debugging. - Turtle Graphics: Call
wn.update()if you want smoother animation (though not required).
Conclusion
You've built a complete tortoise vs. hare simulation using Python's turtle module. This project teaches functions, random number generation, while loops, and graphical output. Experiment with probabilities or add new animals. Happy coding!