I am reading a Python tutorial, one of the exercises has me stuck. The description for the exercise is: "Read the following function and see if you can figure out what it does. Then run it." So I cannot really tell you what it's doing, I'm still working on that.
I only wrote the two first lines myself, it's a copy paste from said tutorial. Here is the code:
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)
Gives traceback:
> 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'
The reason you get this error is because the first parameter is 5
, so the variable t
has the value 5
in the function. The code then attempts to call 5.fd(length*n)
. Switch the first parameter to t
when calling draw
:
draw(t, 10, 15)