【发布时间】:2014-02-14 17:23:04
【问题描述】:
如果您在 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 错误的错误类的名称是什么?
【问题讨论】:
标签: exception graphics exception-handling turtle-graphics