Skip to main content
 首页 » 编程设计

Python Turtle 属性错误 : 'Turtle' object has no attribute 'position'

2024年12月31日16JeffreyZhao

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

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

执行 fd(bob, 100) 行没有问题,但是当我尝试检查 turtle 位置时,它返回 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.TurtleWorld 的 Turtle 类混淆了。

在turtle module ,确实有一个 position() 方法,但是 swampy.TurtleWorld's Turtle class 中没有,正如@Georgy 在评论中提到的。

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