Question

When I try to run the first piece of sample code from the Python documentation on turtle:

from turtle import *
color('red', 'yellow')
begin_fill()
while True:
    forward(200)
    left(170)
    if abs(pos()) < 1:
        break
end_fill()
done()

I get a NameError:

NameError: name 'color' is not defined

Tweaking the import and manually specifying the module doesn't work either:

import turtle
turtle.color('red', 'yellow')
turtle.begin_fill()
while True:
    turtle.forward(200)
    turtle.left(170)
    if abs(turtle.pos()) < 1:
        break
turtle.end_fill()
turtle.done()

I am using Python v3.2.3, which clearly contains turtle.color, per the documentation. Python is installed with tkinter support as well, because import tkinter works as well.

The full trace is:

Traceback (most recent call last):
  File "<path name that contains no spaces>/turtle.py", line 1, in <module>
    from turtle import *
  File "<path name that contains no spaces>\turtle.py", line 2, in <module>
    color('red', 'yellow')
NameError: name 'color' is not defined

How odd. If I enter the shell, either the command line or IDLE, and enter the commands one at a time:

>>> from turtle import *
>>> color('red', 'yellow')

there isn't a problem. It's only when I open a new window in IDLE, enter all of the commands, and run the script.

Était-ce utile?

La solution

You named your file "turtle.py" so when you import turtle, you are importing your own file instead of the stdlib module. Change the name of your program, and delete all the .pyc files in that directory.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top