0
所以我有一个学校项目,我们需要为GPS系统做几个类。我有一个问题搞清楚函数dist(self,other):显示在我的代码底部。项目后期的其他定义很大程度上依赖于它,但我很困惑这一点。位于dist函数计算的曼哈顿距离(X1-X2)+通过实例变量X和ÿ定义的位置的(Y1-Y2),和另一个位置其他其被给定为一个元组python - 麻烦计算manhatan距离,TypeError
class GPS_Location:
def __init__(self,x,y):
self.x=x
self.y=y
def __str__(self):
return '(%s,%s)' % (self.x,self.y)
def __repr__(self):
return 'GPS_Location(%s,%s)' % (self.x,self.y)
def __eq__(self,other):
self.other = other
if (self.x,self.y) == other:
return True
else:
return False
def dist(self,other):
self.other = other
return abs(self.x - (other[0])) + abs(self.y - (other[1])) #TypeError
当测试代码时,我不断收到“TypeError:'GPS_Location'对象不可迭代”。我已经尝试了很多次的调整,但我无法弄清楚我做错了什么。
任何帮助将不胜感激!
请将您的代码添加为代码格式的文本,而不是链接到图像。 – TigerhawkT3
您还应该添加生成错误的代码。什么是“位置”对象? – Francesco
注意:为什么在'dist'和'__eq__'中都写'self.other = other'而没有使用它(你用'other'参数调用这两个函数并在你的代码中使用'other')?或者你在别的地方使用它? – quapka