Learn Special Methods by Building a Vector Space - Step 54

Tell us what’s happening:

dear support team, this is my code (no synthax errors on the console but not pass):
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):
for i in vars(self):
if i in vars(other):
factor+= ({i:getattr(self,i)*getattr(other,i)})
else:
return factor

Your code so far

class R2Vector:
    def __init__(self, *, x, y):
        self.x = x
        self.y = y

    def norm(self):
        return sum(val**2 for val in vars(self).values())**0.5

    def __str__(self):
        return str(tuple(getattr(self, i) for i in vars(self)))

    def __repr__(self):
        arg_list = [f'{key}={val}' for key, val in vars(self).items()]
        args = ', '.join(arg_list)
        return f'{self.__class__.__name__}({args})'

    def __add__(self, other):
        if type(self) != type(other):
            return NotImplemented
        kwargs = {i: getattr(self, i) + getattr(other, i) for i in vars(self)}
        return self.__class__(**kwargs)

    def __sub__(self, other):
        if type(self) != type(other):
            return NotImplemented
        kwargs = {i: getattr(self, i) - getattr(other, i) for i in vars(self)}
        return self.__class__(**kwargs)

# User Editable Region

    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):
            for i in vars(self):
                if i in vars(other):
                    factor+= ({i:getattr(self,i)*getattr(other,i)})
                else:
                    return factor

# User Editable Region

class R3Vector(R2Vector):
    def __init__(self, *, x, y, z):
        super().__init__(x=x, y=y)
        self.z = z

v1 = R2Vector(x=2, y=3)
v2 = R2Vector(x=0.5, y=1.25)
print(f'v1 = {v1}')
print(f'v2 = {v2}')
v3 = v1 + v2
print(f'v1 + v2 = {v3}')
v4 = v1 - v2
print(f'v1 - v2 = {v4}')

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36

Challenge Information:

Learn Special Methods by Building a Vector Space - Step 54

The code not having syntax errors doesn’t mean it has no logical errors. What have you tried to check the logical correctness of the code?

hello [JeremyLT],
thankyou for your response.
I actually tried to iterate through the first vector (self) items and if an equivalent (i) item exists in the second vector(other) so the multiplication happens and the result is added to the variable (factor), otherwise (else) the multiplication process ends and the last reached item(s) multiplication cumulus in “factor” variable is returned.
that was the logic I followed. I don’t know if that makes any sense or it is wrong

But how did you check that the logic is working as expected?

1 Like

Hello JeremyLT,
You got a point and you actually helped me: I declared a v5 vector after the class where I assigned (v5= v1 * v2 and print(f’v1*v2 = {v5}') ) . I kept trying and receiving error back from the console , then I made many attempts changing the logic till I got pass at the end with this fillowing logic/code:
removed by mod
thank you for your implicite direction (assistance).
Kind regards.

2 Likes

Glad you got it! However, please don’t add solution code to the forum

1 Like

thank you pkdvalis,
and sorry for the inconvenience.
your request is well noted.
thanks.

1 Like