Back to Freecodecamp

Step 10

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

latest1.1 KB
Original Source

--description--

You want to return a meaningful string representation. For example, in the case of v1, you want to return a string containing the tuple with the values of the vector components: (2, 3).

From the __str__ method, return a string representing the vector as a tuple containing the vector components in order. Then, go outside the Vector class and print v1 to check the result.

--hints--

Your __str__ method should return a string representing the vector as a tuple containing the vector components in order.

js
({ test: () => runPython(`
v1 = Vector(2, 3)
v2 = Vector(0, 0)
v3 = Vector(-2, -3.5)
assert str(v1) == '(2, 3)'
assert str(v2) == '(0, 0)'
assert str(v3) == '(-2, -3.5)'
`) })

You should print v1.

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

--seed--

--seed-contents--

py
--fcc-editable-region--
class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        
    def norm(self):
        return (self.x**2 + self.y**2)**0.5
        
    def __str__(self):
        pass

v1 = Vector(2, 3)
print(v1.norm())
--fcc-editable-region--