0
我有這樣的代碼運行:turtle.TurtleGraphicsError沒有註冊?
import random
import turtle
turtle.speed(0)
def jump(x,y):
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
#end def
def random_walk(n_steps):
turtle.goto(0,0)
for i in range(n_steps):
leftright = random.randint(0,10)
if leftright<5:
turtle.left(random.randint(0,359))
turtle.forward(random.randint(8,12))
elif 5<leftright:
turtle.right(random.randint(0,359))
turtle.forward(random.randint(8,12))
#end for
#end def
step = int(input("How far would you like your turtle to move?"))
while True:
try:
color = input("And what color would you like your turtle to be?")
break
except turtle.TurtleGraphicsError:
print('Oops! i dont recognize taht color, try another!')
turtle.pencolor(color)
random_walk(step)
我的計劃是讓代碼當一個無效顏色字符串被實施出現停止錯誤但是外殼還是返回此錯誤:
And what color would you like your turtle to be?redf
Traceback (most recent call last):
File "/Users/sgomena/Desktop/122/project 5/project5d.py", line 35, in <module>
turtle.pencolor(color)
File "<string>", line 1, in pencolor
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/turtle.py", line 2252, in pencolor
color = self._colorstr(args)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/turtle.py", line 2696, in _colorstr
return self.screen._colorstr(args)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/turtle.py", line 1158, in _colorstr
raise TurtleGraphicsError("bad color string: %s" % str(color))
turtle.TurtleGraphicsError: bad color string: redf
>>>
我已經研究過它,好像我應該得到一個不同的錯誤,如果代碼運行正常。 在此先感謝您的幫助!
現在能工作嗎? – elegent 2015-03-16 20:41:32
是的!謝謝。 – 2015-04-07 16:40:07