1

I'm getting a colorfill error for my Python turtle graphics code, but I don't understand why. The method t.fillcolor() should accept 3 values as the r,g,b values for the fillcolor of a turtle shape, where t is a turtle. However, any three values in the method produce an error.

This is how the t.fillcolor() is explained in my book:

enter image description here

Here is the code:

from turtle import Turtle,Screen
#might want to use:
#from turtle import *
from random import randint

def hexagon(t, length):
    """Draws a hexagon with the given length."""
    for count in range(6):
        t.forward(length)
        t.left(60)

def radialPattern(t, n, length, shape):
    """Draws a radial pattern of n shapes with the given length."""
    for count in range(n):
        shape(t, length)
        t.left(360 / n)

screen = Screen() #create a screen object
screen.setup(width=600,height=400) #size
screen.bgcolor("lightblue")
screen.title("My turtle")
t = Turtle()

#added code here:
t.fillcolor(20,75,153)
t.begin_fill()
radialPattern(t,5,30,hexagon)
t.end_fill()



screen.exitonclick() #keep the screen open until user closes it

The code execution starts at line screen = Screen() #create a screen object

Here is the error:

Traceback (most recent call last):
  File "D:\Turtle for final.py", line 25, in <module>
    t.fillcolor(20,75,153)
  File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\turtle.py", line 2289, in fillcolor
    color = self._colorstr(args)
  File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\turtle.py", line 2697, in _colorstr
    return self.screen._colorstr(args)
  File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\turtle.py", line 1167, in _colorstr
    raise TurtleGraphicsError("bad color sequence: %s" % str(color))
turtle.TurtleGraphicsError: bad color sequence: (20, 75, 153)

I've gotten this method to work properly plenty of times. It's just not working here and I don't understand why.

If someone could help me out, I would appreciate it.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Spellbinder2050
  • 644
  • 3
  • 13
  • That error shouldn't be possible with those values. I have to suspect that you've somehow modified the previous line in `turtle.py` from the Python standard library, which should read `if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)):`. – jasonharper May 08 '23 at 15:36
  • Do you think the turtle.py module could have gotten corrupted? I've never opened it. – Spellbinder2050 May 08 '23 at 15:42
  • 2
    No wait, I didn't look far enough up from the error location. It wants values in the 0.0 - 1.0 range by default; if you want to use 0 - 255 values, you have to call `screen.colormode(255)` first. – jasonharper May 08 '23 at 15:48
  • Does this answer your question? [what does bad color sequence mean in python turtle?](https://stackoverflow.com/questions/16778324/what-does-bad-color-sequence-mean-in-python-turtle) – ggorlen May 08 '23 at 16:52
  • It was a colormode error, thanks. I had moved the turtle.cfg that had this information in it, and that's what created the issue. – Spellbinder2050 May 08 '23 at 16:57

0 Answers0