Getting error while try to color my turtle

from turtle import Turtle
my_turtle = Turtle()
my_turtle.color(40.0, 80.0, 120.0)
my_turtle.forward(50)

The code works well when I try to work with str such as .color("green") or .color("#285078"), but while I work with the 3 int I get this error:

    Traceback (most recent call last):
  File "E:\Python Projects\Practice\Day 18\main.py", line 4, in <module>
    my_turtle.color(40.0, 80.0, 120.0)
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 2217, in color
    pcolor = self._colorstr(pcolor)
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 2697, in _colorstr
    return self.screen._colorstr(args)
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 1167, in _colorstr
    raise TurtleGraphicsError("bad color sequence: %s" % str(color))
turtle.TurtleGraphicsError: bad color sequence: (40.0, 80.0, 120.0)

>Solution :

Create a Screen object and set its colormode to 255 which on default was set to 1.0.

from turtle import Screen

my_screen = Screen()
my_screen.colormode(255)

Now pass the arguments of my_turtle.color() of integer datatype.

my_turtle.color(40, 80, 120)

Note: At the end of the program, add this line which lets you exit the window after a click, or else it closes automatically as soon it’s opened.

my_screen.exitonclick()

Leave a Reply