龟模块没有属性颜色?

Ric*_*ano 3 python turtle-graphics python-3.2

当我尝试从Python文档中turtle运行第一段示例代码:

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

我得到一个NameError:

NameError:未定义名称"颜色"

调整import并手动指定模块也不起作用:

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()
Run Code Online (Sandbox Code Playgroud)

我正在使用Python v3.2.3,它清楚地包含turtle.color,根据文档.Python也tkinter支持安装,因为import tkinter也可以.

完整的痕迹是:

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
Run Code Online (Sandbox Code Playgroud)

有多奇怪 如果我输入shell,无论是命令行还是IDLE,并一次输入一个命令:

>>> from turtle import *
>>> color('red', 'yellow')
Run Code Online (Sandbox Code Playgroud)

没有问题.只有当我在IDLE中打开一个新窗口时,输入所有命令,然后运行脚本.

Ned*_*der 13

您将文件命名为"turtle.py",因此当您import turtle导入自己的文件而不是stdlib模块时.更改程序的名称,并删除该目录中的所有.pyc文件.