我只是學習使用「Think Python」一書的代碼,我很困惑。我遇到的問題是在TurtleWorld中創建鮮花。我創建的功能在他們的要求上不一致。首先,我發佈的成品,實際工作:功能不一致的要求
from swampy.TurtleWorld import*
world=TurtleWorld()
bob=Turtle()
print bob
bob.delay=.001
def polyline(t,n,length,angle):
t=Turtle
print t
for i in range(n):
fd(bob,length)
lt(bob,angle)
def arc(t, r, angle):
t=Turtle
arc_length=2*math.pi*r*angle/360
n=int(arc_length/3)+1
step_length=arc_length/n
step_angle=float(angle)/n
polyline(t,n,step_length,step_angle)
def petal(t,r,angle):
for i in range(2):
arc(t,r,angle)
lt(t,180-angle)
def flower(t, n, r, angle):
t=Turtle
for i in range(n):
petal(bob,r,angle)
lt(bob,360/n)
flower(bob,5,77,99)
wait_for_user
論arc
和petal
函數定義,t
足夠了烏龜,雖然當我開始,在flower
定義使用t
和polyline
返回一個錯誤未綁定的方法(fd和lt)。需要烏龜實例,取而代之的是類型實例。
將t=Turtle
和print turtle
添加到一半的函數定義後,添加了事實後嘗試修復此錯誤。這是工作版本,我只想知道爲什麼它以前沒有工作。我甚至不知道爲什麼這會起作用,因爲我主要把bob
作爲t
出於沮喪,我沒有真正期待它的工作。
'BOB =龜()''套到bob'評估龜函數的結果。嘗試'bob = Turtle'(注意不要使用圓括號)並刪除t中的函數分配。 – mpez0