• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Creating a Random Color Turtle in Python

Creating a Random Color Turtle in Python

February 26, 2022 Leave a Comment

In Python when using the turtle module, we can easily create a random color turtle with the help of the randint() function.

import turtle
from random import randint

turtle.colormode(255)

t = turtle.Turtle()

t.color(randint(0,255),randint(0,255),randint(0,255))

The Python turtle module provides us with many functions which allow us to add color to the shapes we draw. There are many options for colors in the turtle module which can add life to our designs.

When creating graphics, sometimes it’s cool to be able to generate random colors to make random colored shapes or designs.

We can generate random colors using RGB colors. To use RGB colors, we change the color mode to RGB mode (‘255’), and then we use the randint() function from the random module to generate random numbers in the range 0 to 255.

With the help of the randint() function, we can create a random color turtle in our Python program.

Let’s create a program which will randomly generate a new color for our turtle after each move it makes.

To do so, we just need to call the color() function with three random inputs.

Below is an example in Python of how to get a random color turtle.

import turtle
from random import randint

turtle.colormode(255)

t = turtle.Turtle()

def moveTurtle(x):
    t.color(randint(0,255),randint(0,255),randint(0,255))
    t.forward(5)
    if x % 3 == 0:
        t.right(45)
    else:
        t.left(25)

for x in range(0,100):
    moveTurtle(x)

random color turtle python

Another application of get random color turtles when drawing a shape is creating a spiral which changes color as it gets bigger and bigger.

Below is an example of a spiral that changes color as it gets bigger in Python.

import turtle
from random import randint

turtle.colormode(255)

t = turtle.Turtle()

def draw_spiral(starting_radius, loops):
    for i in range(0, loops):
        t.pencolor(randint(0,255),randint(0,255),randint(0,255))
        t.circle(starting_radius + i, 60)      

draw_spiral(10, 50)

turtle spiral random colors

Hopefully this article has been useful for you to learn how to generate a random color turtle in Python.

Other Articles You'll Also Like:

  • 1.  Sort by Two Keys in Python
  • 2.  Write Float to File Using Python
  • 3.  How to Read XLSX File from Remote Server Using Paramiko FTP and Pandas
  • 4.  Python Remove First Element from List
  • 5.  pandas Drop Columns – Delete Columns from a DataFrame
  • 6.  Python Replace Space With Dash Using String replace() Function
  • 7.  Get Day of Year from Date in pandas DataFrame
  • 8.  How to Output XLSX File from Pandas to Remote Server Using Paramiko FTP
  • 9.  Get Month Name from Date in Python
  • 10.  Using Python to Count Number of True in List

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy