what am i doing wrong here it says you should return a generator expression that attributes over vars(self), ive done that…
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
# User Editable Region
def __str__(self):
return getattr(vars(self),i)
# 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 = R3Vector(x=2, y=2, z=3)
print(v1.norm())
print(v2.norm())
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Challenge Information:
Learn Special Methods by Building a Vector Space - Step 23
I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
This is great, you are 50% of the way there. This step is difficult, a few people have gotten stuck exactly here. I think the instructions need a revision.
This is good, but you need to return a function, you can’t have return in the loop like this or it will end the function the first time the loop hits return.
lol i done it, thank you, it wasn’t working for like 30 mins and i was like wtf but thats because there was a print(v2) at the bottom to help me troubleshoot, thanks for helping, my friend, can i ask do you do this to improve your own code as well and do you recommend it for learning? and by “it” i mean helping others code
Yes, gotta be careful with those troubleshooting print statements, they are useful but must remember to remove them to submit the step.
I guess I do it for multiple reasons. To keep myself sharp, to help other people learn and to return the favour when I’ve needed help and came to the forum. I think it wouldn’t hurt to mention it during a job interview someday. It’s also a bit addictive but at least it’s something constructive and helpful.
You could ask Floyd Mayweather if he does it for the money or the love of the game? Probably both
I did some programming in High School (QBASIC, C++, Assembly) and College (Java). That was over 20 years ago, though. It was never a focus but I was always familiar with the basics.
I started to get more serious about it recently and began the fCC Python curriculum in 2022. I recommend using a variety of resources as well, and try to immerse yourself. When I wasn’t on fCC I would listen to programming podcasts (Code Newbie is good, Coding Blocks, fCC podcast as well), using SoloLearn on my phone during breaks or downtime, doing quizzes on RealPython, etc.
So you’ve been programming for a long time, thanks for the advice too, however i’m here replying to trouble you and ask you how do you trouble shoot in general, cuz it helps alot
Find out what a variable is at a certain point, or find out how an object is structured or if code is doing what you expect or not print()print()print()
You want to know if a loop is executing or not? 1st line in the loop can be
print("loop begins")
If you’re not printing each step it’s all happening invisibly. Using print() at different points in your code is like cutting open the code and putting a magnifying glass up to it so you can see what it’s doing.
Crucial.
Later you’ll need to remove those prints, especially to pass the tests of course. But you are allowed to do whatever you want in the meantime.
Hey,
thank you very much. I was sitting in front of it for ages until I understood that it was because of my vector output. However, I didn’t understand from the task that suddenly no string was required. The whole time I had the output which was required 2-3 steps later. I also think that the problem should be adapted here. It is not obvious that an output as a string should suddenly be turned into something else, only to be turned back into a string 2-3 steps later. The output of the vectors would not have been a problem if a string had been required as intended.