如果在Python的Turtle Graphics中输入了一个无法识别的颜色,系统会回复您,例如:
Traceback (most recent call last):
File "C:\Python33\turtle\ex21-starafterstar.py", line 18, in <module>
color(outline,fillcol) # Set pen and fill colors
File "<string>", line 1, in color
File "C:\Python33\lib\turtle.py", line 2208, in color
pcolor = self._colorstr(pcolor)
File "C:\Python33\lib\turtle.py", line 2688, in _colorstr
return self.screen._colorstr(args)
File "C:\Python33\lib\turtle.py", line 1150, in _colorstr
raise TurtleGraphicsError("bad color string: %s" % str(color))
turtle.TurtleGraphicsError: bad color string: yucky brown
由此可以推断,在'try语句中捕获此类错误的方法是编写代码:
try:
outline = input("Enter the outline color > ")
pencolor(outline)
break
except TurtleGraphicsError:
print("Sorry. I don't recognise that color.")
但这不起作用。它会生成以下错误:
Traceback (most recent call last):
File "C:/Python33/turtle/ex28-star5coloredtry.py", line 17, in <module>
except TurtleGraphicsError:
NameError: name 'TurtleGraphicsError' is not defined
因此,我的问题是,Turtle Graphics错误的错误类的名称是什么?
答案 0 :(得分:0)
与错误消息一样,该范围内未定义名称TurtleGraphicsError
。它在turtle
包中定义。您可以像以下一样导入它:
from turtle import TurtleGraphicsError
或使用它的限定名称
import turtle
try:
...
except turtle.TurtleGraphicsError:
print(...)