Back to Freecodecamp

Step 21

curriculum/challenges/english/blocks/learn-special-methods-by-building-a-vector-space/65f870003444fb1a2ad171f2.md

latest923 B
Original Source

--description--

Modify the two print calls to print the norm of your vectors and verify that the norm() method works fine.

--hints--

You should print v1.norm().

js
({ test: () => assert(runPython(`_Node(_code).has_call("print(v1.norm())")`)) })

You should print v2.norm().

js
({ test: () => assert(runPython(`_Node(_code).has_call("print(v2.norm())")`)) })

--seed--

--seed-contents--

py

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

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

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

class R3Vector(R2Vector):
    def __init__(self, *, x, y, z):
        super().__init__(x=x, y=y)
        self.z = z
--fcc-editable-region--
v1 = R2Vector(x=2, y=3)
v2 = R3Vector(x=2, y=2, z=3)
print(v1.__dict__)
print(v2.__dict__)
--fcc-editable-region--