Answer the question
In order to leave comments, you need to log in
Why doesn't the operator subtract a value?
I encountered a problem when solving the problem that when executing the operator c-=a
, the error c-=a gets out
TypeError
: unsupported operand type(s) for -=: 'type' and 'Vector'
even climbed into the textbook and in the code written there - it is also written!
from math import sqrt
class Vector:
def __init__(self, x=0, y=0, z=0):
self.x = x
self.y = y
self.z = z
def __str__(self):
txt= "<" + str(self.x) + "|"
txt+= str(self.y) + "|"
txt+= str(self.z) + ">"
return txt
def __add__(self, obj): #сложение векторов
t=Vector
t.x = self.x + obj.x
t.y = self.y + obj.y
t.z = self.z + obj.z
return t
def __iadd__(self, obj): #сложение векторов с присваиванием
self=self+obj
return self
def __mul__(self, p): #умножение вектора на вектор или вектора на число
if type(p)==Vector:
res=self.x * p.x + self.y * p.y + self.z * p.z
return res
else: #произведение вектора на число
self.x*=p
self.y*=p
self.z*=p
return self
def __rmul__(self, p): #умножение числа на вектор
return self*p
def __neg__(self):
return Vector(-self.x, -self.y, -self.z)
def __sub__(self, obj):
return -obj+self
def __isub__(self, obj):
self=-obj+self
return self
def __abs__(self):
return sqrt(self*self)
def __truediv__(self, p):
return self*(1/p)
def __eq__(self, obj):
if self.x==obj.x and self.y==obj.y and self.z==obj.z:
return True
else:
return False
def __ne__(self, obj):
return not self==obj
def __lt__(self, obj):
if abs(self)<abs(obj):
return True
else:
return False
def __gt__(self, obj):
if abs(self)>abs(obj):
return True
else:
return False
def __le__(self, obj):
if abs(self)<=abs(obj):
return True
else:
return False
def __ge__(self, obj):
if abs(self)>=abs(obj):
return True
else:
return False
def __invert__(self):
self.x = 10 - self.x
self.y = 10 - self.y
self.z = 10 - self.z
return self
def __lshift__(self, n):
for i in range(n):
self.x, self.y, self.z=self.y, self.z, self.x
return self
def __rlshift__(self, n):
return self>>n
def __rshift__(self, n):
for i in range(n):
self.x, self.y, self.z=self.z, self.x, self.y
return self
def __rrshift__(self, n):
return self<<n
print("Векторы")
a=Vector(1,2,-1)
b=Vector(1,-1,3)
c=Vector(1,2,-1)
print("a =", a)
print("b =", b)
print("c =", c)
print("Модуль вектора")
print("|a| =", abs(a))
print("|b| =", abs(b))
print("|c| =", abs(c))
print("Сравнение векторов")
print("a==b ->", a==b)
print("a!=b ->", a!=b)
print("a==c ->", a==c)
print("a<b ->", a<b)
print("a>b ->", a>b)
print("a<=c ->", a<=c)
print("a>=c ->", a>=c)
print("===============================================")
print("a+b =", a+b)
c+=a
print("c+=a ->", c)
print("a-b =", a-b)
c-=a
print("c-=a ->", c)
print("a*b =", a*b)
print("Умножение и деление вектора на число")
print("a*3 =", a*3)
print("a =", a)
print("2*b =", 2*b)
print("b =", b)
print("a/3", a/3)
print("a =", a)
print("Циклические перестановки:")
v=Vector(1,2,3)
print("v =", v)
print("v<<1 =", v<<1)
print("v>>1 =", v>>1)
print("2>>v =", 2>>v)
print("2<<v =", 2<<v)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question