尝试给我的乌龟上色时出错
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)
当我尝试使用 .color("green") 或 .color("#285078") 等 str 时,代码运行良好,但是当我使用 3 int 时,出现此错误:
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)
创建一个 Screen
对象并将其 colormode
设置为 255
,默认设置为 1.0
。
from turtle import Screen
my_screen = Screen()
my_screen.colormode(255)
现在传递 my_turtle.color()
整数数据类型的参数。
my_turtle.color(40, 80, 120)
注意:在程序末尾添加这一行,让你在点击后退出window,否则一打开就自动关闭.
my_screen.exitonclick()
from turtle import Turtle
my_turtle = Turtle()
my_turtle.color(40.0, 80.0, 120.0)
my_turtle.forward(50)
当我尝试使用 .color("green") 或 .color("#285078") 等 str 时,代码运行良好,但是当我使用 3 int 时,出现此错误:
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)
创建一个 Screen
对象并将其 colormode
设置为 255
,默认设置为 1.0
。
from turtle import Screen
my_screen = Screen()
my_screen.colormode(255)
现在传递 my_turtle.color()
整数数据类型的参数。
my_turtle.color(40, 80, 120)
注意:在程序末尾添加这一行,让你在点击后退出window,否则一打开就自动关闭.
my_screen.exitonclick()