AttributeError: 'int' object has no attribute 'fd'
AttributeError: 'int' object has no attribute 'fd'
我正在阅读 Python 教程,其中一个练习让我卡住了。练习的描述是:"Read the following function and see if you can figure out what it does. Then run it." 所以我不能真正告诉你它在做什么,我还在努力。
前两行是我自己写的,是教程里的复制粘贴。这是代码:
import turtle
t = turtle.Turtle()
turtle.mainloop()
def draw(t, length, n):
if n == 0:
return
angle = 50
t.fd(length*n)
t.lt(angle)
draw(t, length, n-1)
t.rt(2*angle)
draw(t, length, n-1)
t.lt(angle)
t.bk(length*n)
draw(5, 10, 15)
给出回溯:
> Traceback (most recent call last): File
> "D:\Directory\Python\Projects\Learning python\Exercises\Exercise
> 5.14.5.py", line 18, in <module>
> draw(5, 10, 15) File "D:\Directory\Python\Projects\Learning python\Exercises\Exercise 5.14.5.py", line 10, in draw
> t.fd(length*n) AttributeError: 'int' object has no attribute 'fd'
你得到这个错误的原因是因为第一个参数是5
,所以变量t
在函数中的值是5
。然后代码尝试调用 5.fd(length*n)
。调用draw
时将第一个参数切换为t
:
draw(t, 10, 15)
我正在阅读 Python 教程,其中一个练习让我卡住了。练习的描述是:"Read the following function and see if you can figure out what it does. Then run it." 所以我不能真正告诉你它在做什么,我还在努力。
前两行是我自己写的,是教程里的复制粘贴。这是代码:
import turtle
t = turtle.Turtle()
turtle.mainloop()
def draw(t, length, n):
if n == 0:
return
angle = 50
t.fd(length*n)
t.lt(angle)
draw(t, length, n-1)
t.rt(2*angle)
draw(t, length, n-1)
t.lt(angle)
t.bk(length*n)
draw(5, 10, 15)
给出回溯:
> Traceback (most recent call last): File
> "D:\Directory\Python\Projects\Learning python\Exercises\Exercise
> 5.14.5.py", line 18, in <module>
> draw(5, 10, 15) File "D:\Directory\Python\Projects\Learning python\Exercises\Exercise 5.14.5.py", line 10, in draw
> t.fd(length*n) AttributeError: 'int' object has no attribute 'fd'
你得到这个错误的原因是因为第一个参数是5
,所以变量t
在函数中的值是5
。然后代码尝试调用 5.fd(length*n)
。调用draw
时将第一个参数切换为t
:
draw(t, 10, 15)