Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading