3

上の Python ドキュメントturtleからサンプル コードの最初の部分を実行しようとすると、次のようになります。

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

私は得るNameError

NameError: 名前 'color' が定義されていません

モジュールを微調整し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()

ドキュメントに従って、明らかに含まれているPython v3.2.3を使用してturtle.colorいます。Pythontkinterも同様に機能するため、サポート付きでインストールさ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

奇妙です。コマンド ラインまたは IDLE のいずれかでシェルに入り、一度に 1 つずつコマンドを入力すると、次のようになります。

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

問題ありません。IDLE で新しいウィンドウを開き、すべてのコマンドを入力して、スクリプトを実行したときだけです。

4

1 に答える 1

15

ファイルに「turtle.py」という名前を付けたのでimport turtle、 stdlib モジュールではなく独自のファイルをインポートしています。プログラムの名前を変更し、そのディレクトリ内のすべての .pyc ファイルを削除します。

于 2012-07-05T16:29:01.900 に答える