Mark F writes:
I think that it's a great book but I am running into one issue. If you're not too busy, I'm hoping you could answer it for me. In Chapter 8, Classes and Objects, you reintroduce the turtle module. You say that we can create 'an object of the pen class,' which makes sense.
import turtle
avery = turtle.pen()
kate = turtle.pen()However, when I attempt to move the turtle using either one of those objects, I get this error:
avery.forward(50)
Traceback (most recent call last):
File "\<pyshell#3>", line 1, in
avery.forward(50)
AttributeError: 'dict' object has no attribute 'forward'In fact, the only time I can get the turtle to move is if i simply type in turtle.forward(). I am using version 3.3 but have tried with 3.2 and encounter the same error. Please let me know why I keep getting this error so that I can keep moving forward.
If you compare your code snippet above, with the example in the book, there's one fairly obvious difference: lowercase "p" versus uppercase "P". The question is, why is it such a problem?
In the turtle module, Pen
and pen
are two distinct things. We can see the difference if we run the Python console (or Shell), and try out the following code:
>>> import turtle
>>> turtle.pen
<function pen at 0x10117fe60>
>>> turtle.Pen
<class turtle.Turtle>
Without the brackets (i.e. without entering pen()
or Pen()
), Python just prints out a simple description, so we can see that pen
is a function, and Pen
is a class (good rule of thumb: names which start with an uppercase letter are generally classes, names with a lowercase letter are functions, variables, and so on). For the example code, we want to create a Pen
object (we don't want to call the pen
function) - so if you change your code to...
import turtle
avery = turtle.Pen()
kate = turtle.Pen()
...you should have a bit more success.