sususususu 发表于 2023-10-20 18:34:02

TypeError: unsupported operand type(s),求大佬解惑

class Vector3D:
    def __init__(self,x,y,z):
      self.x=x
      self.y=y
      self.z=z
    def __add__(self,other):
      return Vector3D(self.x+other.x,self.y+other.y,self.z+other.z)
    def __sub__(self,other):
      return Vector3D(self.x-other.x,self.y-other.y,self.z-other.z)
    def __mul__(self,scalar):
      return Vector3D(self.x*scalar,self.y*scalar,self.z*scalar)
    def __div__(self,scalar):
      if scalar==0:
            raise ValueError("Division by zero is not allowed.")
      return Vector3D(self.x/scalar,self.y/scalar,self.z/scalar)
    def __str__(self):
      return f"({self.x},{self.y},{self.z})"

v1=Vector3D(1,2,3)
v2=Vector3D(4,5,6)
result_add=v1+v2
print("向量加法:",result_add)
result_sub=v1-v2
print("向量减法:",result_sub)
scalar=2
result_mul=v1*scalar
print("向量与标量乘法:",result_mul)
result_div=v1 / scalar   //TypeError: unsupported operand type(s) for /: 'Vector3D' and 'int'
print("向量与标量的除法:",result_div)

报错见于倒数第二行,为什么乘法没报错,除法报错了呢?

isdkz 发表于 2023-10-20 18:34:48

问题出在__div__方法上。在 Python 3 中, __div__方法已经被移除,取而代之的是__truediv__方法。因此,你需要将__div__方法改为__truediv__方法。

修改后的代码如下:

class Vector3D:
    def __init__(self, x, y, z):
      self.x = x
      self.y = y
      self.z = z

    def __add__(self, other):
      return Vector3D(self.x + other.x, self.y + other.y, self.z + other.z)

    def __sub__(self, other):
      return Vector3D(self.x - other.x, self.y - other.y, self.z - other.z)

    def __mul__(self, scalar):
      return Vector3D(self.x * scalar, self.y * scalar, self.z * scalar)

    def __truediv__(self, scalar):
      if scalar == 0:
            raise ValueError("Division by zero is not allowed.")
      return Vector3D(self.x / scalar, self.y / scalar, self.z / scalar)

    def __str__(self):
      return f"({self.x}, {self.y}, {self.z})"

v1 = Vector3D(1, 2, 3)
v2 = Vector3D(4, 5, 6)
result_add = v1 + v2
print("向量加法:", result_add)
result_sub = v1 - v2
print("向量减法:", result_sub)
scalar = 2
result_mul = v1 * scalar
print("向量与标量乘法:", result_mul)
result_div = v1 / scalar
print("向量与标量的除法:", result_div)


这样修改后,运行时不会再出现TypeError: unsupported operand type(s) for /: 'Vector3D' and 'int'错误。

陶远航 发表于 2023-10-20 18:34:51

在Python中,当你尝试对一个自定义的类进行除法操作时,你需要定义一个名为__truediv__的特殊方法,而不是__div__。这是因为在Python 3中,/操作符对应的是__truediv__方法,而不是__div__方法。__div__方法在Python 2中被用于除法,但在Python 3中已经被弃用。

所以,你需要将你的__div__方法改为__truediv__,如下所示:

class Vector3D:
    def __init__(self,x,y,z):
      self.x=x
      self.y=y
      self.z=z
    def __add__(self,other):
      return Vector3D(self.x+other.x,self.y+other.y,self.z+other.z)
    def __sub__(self,other):
      return Vector3D(self.x-other.x,self.y-other.y,self.z-other.z)
    def __mul__(self,scalar):
      return Vector3D(self.x*scalar,self.y*scalar,self.z*scalar)
    def __truediv__(self,scalar):# 修改这里
      if scalar==0:
            raise ValueError(\"Division by zero is not allowed.\")
      return Vector3D(self.x/scalar,self.y/scalar,self.z/scalar)
    def __str__(self):
      return f\"({self.x},{self.y},{self.z})\"

v1=Vector3D(1,2,3)
v2=Vector3D(4,5,6)
result_add=v1+v2
print(\"向量加法:\",result_add)
result_sub=v1-v2
print(\"向量减法:\",result_sub)
scalar=2
result_mul=v1*scalar
print(\"向量与标量乘法:\",result_mul)
result_div=v1 / scalar# 这里不再报错
print(\"向量与标量的除法:\",result_div)


这样,你的代码就可以正常运行了,不会再出现\"TypeError: unsupported operand type(s) for /: 'Vector3D' and 'int'\"这样的错误。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案

Mike_python小 发表于 2023-10-20 18:35:18

问题分析:

根据提供的报错信息和代码,可以看出在实现向量与标量(scalar)的除法时发生了错误。报错信息显示不支持使用“/”运算符进行这种操作。

原因是代码中的__div__方法无法正确执行。此方法应该在除法时被调用,但它没有被正确地实现。Python中用/表示除法,但是在Python 3.X中,__div__(self, scalar)方法已被废弃,应该使用__truediv__(self, scalar)方法来代替。如果要保持向后兼容性,可以同时实现这两种方法。

解决方案:

为了修复这个错误,我们可以简单地更改__div__方法到__truediv__方法,或者同时实现这两种方法以保持向后兼容性。为了让代码更加整齐,我们可以通过修改代码来避免将两个方法分开实现的情况。修改后的代码如下所示:


class Vector3D:
    def __init__(self,x,y,z):
      self.x=x
      self.y=y
      self.z=z
      
    def __add__(self,other):
      return Vector3D(self.x+other.x,self.y+other.y,self.z+other.z)
      
    def __sub__(self,other):
      return Vector3D(self.x-other.x,self.y-other.y,self.z-other.z)
      
    def __mul__(self,scalar):
      return Vector3D(self.x*scalar,self.y*scalar,self.z*scalar)
      
    def __truediv__(self,scalar):
      if scalar == 0:
            raise ValueError("Division by zero is not allowed.")
      return Vector3D(self.x/scalar,self.y/scalar,self.z/scalar)

    def __str__(self):
      return f"({self.x},{self.y},{self.z})"
      
v1 = Vector3D(1,2,3)
v2 = Vector3D(4,5,6)
result_add = v1 + v2
print("向量加法:",result_add)
result_sub = v1 - v2
print("向量减法:",result_sub)
scalar = 2
result_mul = v1 * scalar
print("向量与标量乘法:",result_mul)
result_div = v1 / scalar
print("向量与标量的除法:",result_div)


这个修改应该可以修复原始代码中的问题。现在,__truediv__方法将在除法时被正确地调用,代码将成功执行。

希望我的解答能够解决你的问题。如果你还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:}
页: [1]
查看完整版本: TypeError: unsupported operand type(s),求大佬解惑