Python 把一只海龟移到另一只海龟身上

Python 把一只海龟移到另一只海龟身上,python,python-3.x,turtle-graphics,Python,Python 3.x,Turtle Graphics,我正在写一个程序,需要一只海龟移动到另一只海龟。然而,问题很简单: turtle.goto(other_turtle.pos()) 乌龟和其他乌龟是两种已经定义的乌龟 这不起作用,因为它说other\u turtle.pos()是一个方法,并且没有定义方法和浮点之间的减法。因此,我继续尝试将other\u-turtle.pos()更改为float(other\u-turtle.pos())。然后错误的说你不能浮动一个方法 我的问题:这是可能做到的,还是因为python海龟本身的局限性 代码:

我正在写一个程序,需要一只海龟移动到另一只海龟。然而,问题很简单:

turtle.goto(other_turtle.pos())
乌龟和其他乌龟是两种已经定义的乌龟

这不起作用,因为它说
other\u turtle.pos()
是一个方法,并且没有定义方法和浮点之间的减法。因此,我继续尝试将
other\u-turtle.pos()
更改为
float(other\u-turtle.pos())
。然后错误的说你不能浮动一个方法

我的问题:这是可能做到的,还是因为python海龟本身的局限性

代码:

错误消息:

Traceback (most recent call last):
  File "C:\Users\gularson\Documents\Garrett\Messing Around\turtle_functions.py", line 97, in <module>
    straight_curve(turt,5,30,["white"])
  File "C:\Users\gularson\Documents\Garrett\Messing Around\turtle_functions.py", line 87, in straight_curve
    turtle.goto(turtles[0].xcor,turtles[0].ycor)
  File "C:\Users\gularson\Documents\Garrett\Python\lib\turtle.py", line 1776, in goto
    self._goto(Vec2D(x, y))
  File "C:\Users\gularson\Documents\Garrett\Python\lib\turtle.py", line 3165, in _goto
    diff = (end-start)
  File "C:\Users\gularson\Documents\Garrett\Python\lib\turtle.py", line 262, in __sub__
    return Vec2D(self[0]-other[0], self[1]-other[1])
TypeError: unsupported operand type(s) for -: 'method' and 'float'
回溯(最近一次呼叫最后一次):
文件“C:\Users\gularson\Documents\Garrett\Messing\turtle\u functions.py”,第97行,在
直线曲线(turt,5,30,[“白色”])
文件“C:\Users\gularson\Documents\Garrett\Messing\turtle\u functions.py”,第87行,直线曲线
海龟.goto(海龟[0].xcor,海龟[0].ycor)
文件“C:\Users\gularson\Documents\Garrett\Python\lib\turtle.py”,第1776行,在goto中
self._goto(Vec2D(x,y))
文件“C:\Users\gularson\Documents\Garrett\Python\lib\turtle.py”,第3165行,在
差异=(结束-开始)
文件“C:\Users\gularson\Documents\Garrett\Python\lib\turtle.py”,第262行,在__
返回Vec2D(自[0]-其他[0],自[1]-其他[1])
TypeError:-:“方法”和“浮点”的操作数类型不受支持
表达式:

turtle.goto(other_turtle.pos())
确实有效。以下操作将正常运行:

import turtle

other_turtle = turtle.Turtle()

turtle.goto(other_turtle.pos())

turtle.done()
但当然,这不是很有趣(将一只乌龟移动到(0,0)的位置,而另一只乌龟已经移动到(0,0)的位置,但这是合法的。)所以一定有别的东西绊倒了你

您在代码中实际编写的内容是:

turtle.goto(other_turtle.xcor, other_turtle.ycor)

turtle.goto(turtles[0].xcor,turtles[0].ycor)
这将导致在传递方法
xcor
ycor
而不是调用它们时报告的错误。应该是:

turtle.goto(other_turtle.xcor(), other_turtle.ycor())

turtle.goto(turtles[0].xcor(), turtles[0].ycor())
或者您询问的原始代码:

turtle.goto(other_turtle.pos())

turtle.goto(turtles[0].pos())

工作正常。

始终对完整错误消息(回溯)提出疑问-它可能比您的描述更有用。您确定要在
pos()
中使用
()
?从您的描述来看,您使用的是
turtle.goto(other_turtle.pos)
而不是
()
。最好显示完整的错误消息和代码。我定义了带名称移动的海龟,即给定的输入,但它移动到的海龟没有名称,而是在列表中定义的。@GarrettGularson,我已经更新了我的答案,因为有足够的上下文来查看代码中的错误。
turtle.goto(other_turtle.pos())

turtle.goto(turtles[0].pos())