I have tried everything but the code doesn’t seem to workout, I have tried taking sum separately, using assignment operators but it is not working. What am I doing wrong?
Error: Sorry, your code does not pass. You’re getting there.
You should implement the scalar product calculation and return the result inside the elif
body of your __mul__
method.
Question:
Within the elif
clause, implement the above formula to compute the result of a scalar product and return the result. Remember that your implementation must be valid for any vector, independently from the number of components, when the method is inherited.
def __mul__(self, other):
if type(other) in (int, float):
kwargs = {i: getattr(self, i) * other for i in vars(self)}
return self.__class__(**kwargs)
elif type(self) == type(other):
kwargs = sum(getattr(self,i) * getattr(other,i) for i in vars(self))
return kwargs