Yuxiao
1
Tell us what’s happening:
Hi, guys! can anyone help with this step? I don’t understand the command, I am confused
Your code so far
# User 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):
return (f'self.x, self.y')
v1 = Vector(2, 3)
print(v1.norm())
print(v1)
# User Editable Region
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
Challenge Information:
Learn Special Methods by Building a Vector Space - Step 10
What is the output of this command? You should see it in the preview area.
Yuxiao
3
The strings of line 10. I know it’s not right, but I just don’t understand the commands and I don’t know how to fix it .
Well, you are printing a string, an f-string. You can either print variables instead of a string, or your can use the f-string to print variables.
https://www.geeksforgeeks.org/formatted-string-literals-f-strings-python/
return a string representing the vector as a tuple containing the vector components in order.
However, this output is very specific. First it wants you to make a tuple()
and then convert that tuple to a string using str()
.
Yuxiao
5
sorry, I don’t understand. I wrote ‘’ return ‘vector()’ ‘’ now. but it still doesn’t pass
Did you complete all of the Python curriculum up to this stage?
There is no vector()
, the instructions say “return a string”
You’ve created a Vector object already:
v1 = Vector(2, 3)
Return a tuple converted into a string. A tuple looks like this:
(var1, var2)
convert it to a string using str()
and return it.
Anything in quotes is a string. You’re converting a string into a string with str()
. What does it show in the preview?
You should know it won’t pass if it shows “self.x” in the preview. You need to print the variable values.
Let me know if you have any questions!