-2

Here is my python code:

import turtle

def square(t):
    t = turtle.Turtle()
    for i in range(4):
        t.fd(100)
        t.lt(90)

square(bob)

When I run my code I get the error:

Traceback (most recent call last):
  File "/home/j/python_projects/tp_ex_4.1.py", line 10, in <module>
    square(bob)
NameError: name 'bob' is not defined

I am learning python programming from the book "Think Python How to think like a computer scientist" and I don't understand why my code will not run. I am trying to write a function called square that takes a parameter named t, which is a turtle. I use a for loop to draw a square with the turtle. Then I write a function call that passes bob as an argument to square. Shouldn't bob replace t in my function? Thereby making bob = turtle.Turtle()? What am I missing?

Axe319
  • 4,255
  • 3
  • 15
  • 31
J. Hawker
  • 3
  • 2
  • 1
    You can pass `bob` as a parameter, but you never defined `bob` previously. However, looking at your code, you don't _need_ to pass a parameter, you define `t` locally as a turtle, so nothing needs to be passed. – B Remmelzwaal May 08 '23 at 18:36
  • bob is a variable, "bob" is a string so change bob to "bob" square("bob") – Ghassen Sultana May 08 '23 at 18:36
  • You're passing `bob` _into_ the function. So effectively you're assigning the value of `bob` to `t`. If you need to pass the value of `t` back out into global scope, look into the `return` statement. – Axe319 May 08 '23 at 18:36
  • what is ``bob``? What yre you trying to do? Why are you having a ``t`` in your function in the first place if you redefien it in the very first line? – Psytho May 08 '23 at 18:36
  • 1
    [This](https://nedbatchelder.com/text/names.html) or [this question](https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference) would also be a helpful read. What you're expecting is _pass by reference_ which Python does not support. – Axe319 May 08 '23 at 18:44
  • You really must explain what you are *expecting* to happen here. – juanpa.arrivillaga May 08 '23 at 19:13

2 Answers2

2

bob is not defined. In this function, you don't even need a parameter since parameters are used to pass values from outer context to the function context, while here you define t in the function itself. Change def square(t): to def square(): and square(bob) to square()

Lior v
  • 464
  • 3
  • 12
-2

Quite obviously: bob is not defined. It looks like a variable that didn't get any value.

You might have wanted to write to the last line something like this:

square('bob')

This way 'bob' is a literal and it will work.

Gyula Sámuel Karli
  • 3,118
  • 2
  • 15
  • 18
  • 3
    the code will work yes, but "bob" has no sense in this code – Psytho May 08 '23 at 18:39
  • I think with a little closer inspection you would see that you _cannot_ pass a string as a parameter to that function. – B Remmelzwaal May 08 '23 at 18:39
  • Why will Python pass the literal 'bob' into the function but will not pass the variable bob? – J. Hawker May 08 '23 at 18:55
  • 1
    @J.Hawker **because there is no variable `bob` defined**. That is precisely what a `NameError` is doing. It's no different than `print(banana)`, you will get a `NameError` complaining that `banana` is not defined (unless of course, you have actually defined it somehwere in scope) – juanpa.arrivillaga May 08 '23 at 19:13
  • 1
    "This way 'bob' is a literal and it will work." it only "works" in the sense that it doesn't throw an error, but this **doesn't do anything useful** – juanpa.arrivillaga May 08 '23 at 19:23
  • 1
    The chapter I'm in currently is about using the turtle module, defining functions, and using for loops. The point of the assignment was to pass an argument into a function. That is why I'm using 'bob' as the argument. Unfortunately, I didn't understand why my initial code failed because I was trying to use a variable that had not been assigned. Thanks for the help. – J. Hawker May 08 '23 at 22:59