wq_
wq_

Reputation: 27

Think Python - Exercise 4.3 #2

These are the questions. I successfully completed Question 1 but I am just adding it for context. I am having trouble with Question 2.

1. Write a function called square that takes a parameter named t, which is a turtle. It
should use the turtle to draw a square.
Write a function call that passes bob as an argument to square, and then run the
program again.
2. Add another parameter, named length, to square. Modify the body so length of the
sides is length, and then modify the function call to provide a second argument. Run
the program again. Test your program with a range of values for length.

Here is my work:

import turtle
bob = turtle.Turtle()
print(bob)

def square(t, length):
    for i in range(4):
        bob.fd(t, length)
        bob.lt(t)

square(bob, 200)

turtle.mainloop()

The following is the traceback:

Traceback (most recent call last):
  File "/Users/ivan/Documents/Python/thinkpython/square.py", line 10, in <module>
    square(bob, 200)
  File "/Users/ivan/Documents/Python/thinkpython/square.py", line 7, in square
    bob.fd(t, length)
TypeError: forward() takes 2 positional arguments but 3 were given

The part I am not understanding in the traceback is that I only see two arguments given to bob.fd() but it's saying that it received three. Can someone explain this situation?

Upvotes: 0

Views: 4024

Answers (2)

OLIVER.KOO
OLIVER.KOO

Reputation: 5993

The forward function fd only takes one argument distance

turtle.fd(distance) Parameter:
distance – a number (integer or float) Move the turtle forward by the specified distance, in the direction the turtle is headed.

  1. In your case, you should only use your turtle instance bob and call fd with one argument in it like t.fd(length)
  2. Also your turtle left turn function takes an integer or float not a turtle instance. so it should be like t.lt(90) if you want the turtle to turn 90 degrees.
  3. your question asked you to pass in a turtle as as a parameter t. that is your turtle instance. That is why instead of calling bob.fd() and bob.lt we are calling t.fd() and t.lt().

    import turtle
    bob = turtle.Turtle()
    print(bob)
    
    def square(t, length):
        for i in range(4):
            t.fd(length)
            t.lt(90)
    
    square(bob, 200)
    
    turtle.mainloop()
    

the above code should have the turtle bob draw a square of 200 by 200. Hope this helps. If you still don't understand leave a comment I will explain it in more detail.

Upvotes: 0

TheoretiCAL
TheoretiCAL

Reputation: 20571

Because bob is an instantiation of the Turtle class and fd is a class function, there is an implicit self passed when the function is called. If you were to look at the definition for fd in the Turtle class, you would see something like def fd(self, distance). When calling the class function bob.fd(t, length), the self argument is passed with the instantiation of the class implicitly, and then you are passing 2 additional arguments (t,length), for a total of 3 arguments.

Upvotes: 1

Related Questions