Tell us what’s happening:
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
Two questions for you:
- Where is the variable
i
defined? Where does it’s value come from?
- What is a generator function and what makes this one?
The instructions state:
iterates through the object attributes
There’s no iteration here, no loop or logic that would go through a range of values. You can also find out what getattr()
returns.
Put this at the end of your code:
print(v2)
And you’ll see what error is generated by your __str__
function, it can help you troubleshoot.
very well explained but im still struggling
def __str__(self):
for i in vars(self):
return getattr(self,i)
ive got this now and i still get the same error
see i think my issue is in the parenthisis in fact im sure it is but i’ve treid every single fucking combo and it don’t work
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.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').
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.
You need to combine these two lines and change the for
loop into a comprehension:
https://www.w3schools.com/python/python_lists_comprehension.asp
Structure it like this:
return (getattr() list comprehension)
I hope this helps. This might be too much of a clue but people do seem to be getting stuck on this step.
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
1 Like
yeah, very good point. I’ll happily sent a testimonial your way if needed, i always see you help others, how long have you been coding for??
1 Like
It’s a bit hard to answer, it’s been a long time but never that deeply.
I did some programming in BASIC when I was very young using this book on a VIC-20: https://archive.org/details/Computer_Space_Games/mode/2up
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.
1 Like
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
1 Like
The most basic step is to print()
everything.
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.
1 Like