Python Turtle AttributeError: 'Turtle' object has no attribute 'position'

Python Turtle AttributeError: 'Turtle' object has no attribute 'position'

我有以下代码可以在 turtleworld 中玩乌龟:

>>> from swampy.TurtleWorld import *
>>> world = TurtleWorld()
>>> bob = Turtle()
>>> fd(bob, 100)
>>> bob.position()
>>> bob.reset()

执行 fd(bob, 100) 行没有问题,但是当我尝试检查海龟位置时 returns AttributeError:

Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    bob.position()
AttributeError: 'Turtle' object has no attribute 'position'

为什么会这样?我错过了什么?我已阅读文档,它指出 position()reset() 方法都应该可用。

您可能混淆了 turtle 模块与 swampy.TurtleWorldTurtle class。

turtle module, there is indeed a position() method, however there isn't in swampy.TurtleWorld's Turtle class 中,正如@Georgy 在评论中提到的那样。

您可以改为使用 bob.get_x()bob.get_y() 作为其 x、y 坐标。