1. 程式人生 > 其它 >AttributeError: ‘Turtle’ object has no attribute ‘done’

AttributeError: ‘Turtle’ object has no attribute ‘done’

技術標籤:turtle模組遞迴演算法影象繪製python遞迴法

AttributeError: ‘Turtle’ object has no attribute ‘done’

AttributeError: ‘Turtle’ object has no attribute ‘done’:

我開始準備使用turtle模組繪製一個螺旋線,但是最後繪製的影象沒法停留,就是沒法看到結果,只能看見過程。
這是原始碼

import turtle
t=turtle.Turtle()
def test_turtle(object,len):#測試turtle模組繪製螺旋線
    if len>=
0: t.forward(len) t.right(90) test_turtle(object,len-2) test_turtle(turtle,20) t.done()#做圖結束

#報錯,出現這個問題AttributeError: ‘Turtle’ object has no attribute ‘done’:

後面結果查詢可能是由以下兩個錯誤引起的:

1.直接turtle=turtle.Turtle()這樣進行類的命名
2.這也是最可能的錯誤
done是turtle模組的功能,而不是turtle.Turtle類的方法
因此我們應該是這樣表示,直接用turtle模組呼叫這個done函式,不能用類的命名去呼叫

turtle.done()#做圖結束

這是改進後的原始碼,能夠看見結果:

import turtle
t=turtle.Turtle()
def test_turtle(object,len):#測試turtle模組繪製螺旋線
    if len>=0:
         t.forward(len)
         t.right(90)
         test_turtle(object,len-2)
test_turtle(turtle,20)
turtle.done()#做圖結束