我的任务有问题。根据问题的条件,给出了具有 xyz 坐标的两个点 p1、p2,您需要使用该类求出这些点在 3D 空间中的距离。看起来任务本身很简单,但对我来说困难在于距离的计算必须使用只有一个距离(其他)参数的方法来进行,而我不\xe2\x80\x99不明白这是怎么回事如果需要两个变量就可以完成,这两个变量将给出两个点的坐标,并且在该方法中我们只能使用一个。
\n我尝试这样做,但出现错误(不支持的操作数类型 -:'str' 和 'str'):
\nfrom math import sqrt\n\n\nclass Point3D:\n x: float\n y: float\n z: float\n\n def __init__(self, x, y, z):\n self.x = x\n self.y = y\n self.z = z\n\n @staticmethod\n def distance(other):\n return sqrt((other[0][0] - other[1][0]) ** 2 + (other[0][1] - other[1][1]) ** 2 + (other[0][2] - other[1][2]) ** 2)\n\np1 = [1, 2, 3]\np2 = [3, 2, 1]\ns1 = Point3D(*p1)\ns2 = Point3D(*p2)\nprint(Point3D.distance((s1, s2)))\n\n>>>unsupported operand type(s) for -: 'str' and 'str'\nRun Code Online (Sandbox Code Playgroud)\n我也尝试这样做,但它给出了一个错误(“str”对象没有属性“x”) …