Attribute error. How to fix it?

import turtle
t = turtle.pen()
t.forward(60)
Traceback (most recent call last):
File “<pyshell#2>”, line 1, in
t.forward(60)
AttributeError: ‘dict’ object has no attribute ‘forward’

I reinstalled python, but it didn’t work. Please help

Hello Denezhkin, and welcome!

You shouldn’t reinstall Python every time you have an error. If I
reinstalled Python every time I got an error, I would be
reinstalling about 300 times a day. Once you have Python installed, an
error just means you have made a mistake, not that Python is broken.

You have tried this:

import turtle
t = turtle.pen()
t.forward(60)

and got an exception

AttributeError: 'dict' object has no attribute 'forward'`

because the turtle’s pen() function returns a dict, not the turtle. So
your t variable is not the turtle, but just a dict holding the pen
settings. If in doubt, try printing t to see what it holds:

print(t)

Try this instead:

turtle.forward(60)

Alternatively, you can do this instead:

t = turtle.Turtle()
t.forward(60)

You can read the docs to see what functions do:

https://docs.python.org/3/library/turtle.html

and some tutorials:

https://duckduckgo.com/?q=python+turtle+tutorial

Don’t forget the Python tutorial:

https://docs.python.org/3/tutorial/index.html

Best wishes!

Steven

3 Likes

thanks dude your explanation is perfect

It’s a typo. The second line should read t = turtle.Pen(). The “P” should be capitalized!

Since I was already an experienced programmer when I first used Python, I never really looked into the turtle module. Apparently. Pen is an alias for the Turtle class itself; but pen is a function that returns a dictionary of properties of the default instance… ? That seems… less than ideal.