Learn Special Methods by Building a Vector Space - Step 54

Tell us what’s happening:

Either I do not understand the request or do not know how how to implement this to satisfy the code check.
help!
v1 = (2, 3)
v2 = (0.5, 1.25)
v3 = v1 * v2

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): 
            print( {i: getattr(self, i) *  getattr(other, i) for i in vars(self)} )
            kwargs = 0
            for l in {getattr(self, i) *  getattr(other, i) for i in vars(self)} : 
                kwargs+= l  
           
            print(kwargs)  
            return self.__class__(**kwargs)

# User Editable Region

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

using the following:
v1 = (2, 3)
v2 = (0.5, 1.25)
v3 = v1 * v2

The code below returns a valid tuple - {‘x’: 1.0, ‘y’: 3.75}. Is a valid overload of multiply with a valid return.
Works when I create and multiple a new vector object in a series (v1 * v2 * v3)

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 =  ( {i: getattr(self, i) *  getattr(other, i) for i in vars(self)} ) 
            return self.__class__(**kwargs) 

Conversly the following is my implemenation of scalar product.
(2 .05) + (3 1.25) = 1.0 + 3.75 = 4.75
Returns the correct product 4.75.
The code returns the error - typeError: main.R2Vector() argument after ** must be a mapping, not float

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 l in {getattr(self, i) *  getattr(other, i) for i in vars(self)} ): 
                kwargs+= l  
    return self.__class__(**kwargs)

type or paste code here

Not sure how to resolve the request and pass the ‘Check your Code’.

Your browser information:

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

Challenge Information:

Learn Special Methods by Building a Vector Space - Step 54
https://www.freecodecamp.org/learn/scientific-computing-with-python/learn-special-methods-by-building-a-vector-space/step-54`Preformatted text`

each component of a is multiplied by the correspondent component of b, and all the products are summed together, resulting in a number.

You want to return 1 number: multiply each pair and add it all together.

This is a dictionary, not a tuple. You don’t want to return components like this either, but a single number, the sum of all the multiplications.

You were closer the second time, but:

  • You don’t want a dictionary, so don’t use curly {braces}
  • careful with your syntax. You could experiment in a Google Colab notebook to get the correct syntax and output before putting it into this function.

I would really recommend experimenting in a different editor where you can explore without messing up your function.

still confused:
u stated"
You don’t want to return components like this either, but a single number, the sum of all the multiplications."
the function value returned is the constructor to the current class. The class constructor expects 2 arguments. In returning the sum of the production, a single number,
I generate the following:
TypeError: main.R2Vector() argument after ** must be a mapping, not float

t 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 self.__class__( **kwargs)

The instructions don’t say anything about returning a class.

“all the products are summed together, resulting in a number.”
“return the result”

thanks . Bad on me. I assumed it would follow the same pattern as the other built in overloads.
Again thanks.

1 Like

Makes sense. A bit more explicit instructions here on what exactly to return wouldn’t hurt.