re ps9: are two triangles 'equal' if they have the same base and same height or the same area? for instance should I consider a triangle with base 2 and height 3 == to a triangle with base 3 and height 2
they didn't specify so you get to decide what equality means. it is a shape object - which might imply what you see when you look at it rather than what number you get when you apply a formula to it.
I am still trying to decide what data structure to use for shapeset. I tried to look up some of the code you posted for this problem but it expired. Would you mind posting again?
Not sure if it will help (I'm about to start PS9) but the geometrical definition of triangle equality is that two triangles are equal if their sides are equal. This will naturally mean they have the same area, same angles etc as well. If two triangles have the same angles but different lengths of sides, they are called similar. If they have the same area but different angles/sides, I'm not sure if there's a term for that, but it's not the same as equality.
bwCA: how do I call the __str__ functions from circle, triangle or square in my shapeset module? http://codepad.org/BI2c6hH0
>>> a = Square(2) >>> print a Square with side 2.0 http://docs.python.org/reference/datamodel.html#object.__str__
i used a dictionary with the __str__ as the key :)
@skibum hey mate, did you sort out how to call those str modules? i've got the rest working and have tried all the 'obvious' formatting options, but just can't nail it. Cheers.
which part of the __str__ function is giving you trouble? The basic rule of str is to return a string; usually I start with an empty string and then use addition to add on whatever is relevant; for objects that use lists it often helps to use a for loop. Here's a really basic example: stringtoreturn = "" for item in self.list: stringtoreturn += item + "\n" #the "\n" will make a new line return stringtoreturn
Oh, for non-list type of items, you might not start with an empty string, but rather use a kind of template, like so: return "basketball player named " + self.name + " with height " + str(self.height) I forgot to mention you have to use the str() function to make any numbers or other non-string values into strings before you add them to your output string.
@thinkpad20 so that all makes sense, but what i'm doing is calling the __str__ of the shape methods inside the ShapeSet class. The problem was i wasn't using the str() function, so now it's working fine, i guess in this case the str() is not actually converting to a string, but telling the shape class that it is requesting a string? i'm not sure exactly how to put this, the structure is logical, but i'll need to familiarise myself with it through practice before i fully grasp it i think. Thanks for the Help!
I'm suffering from an annoying problem, i can't figure out why this isn't working properly! In the __str__ of ShapeSet() i have: self.members.sort() for sh in self.members: try: print str(sh) except TypeError: break I just had the print there originally, but after it prints out the set members, it throws, TypeError: __str__ returned non-string (type NoneType) the try/except has not fixed this and i am stumped. I really want to move on to the next problem, arghhhh! :)
the __str__ function is meant to return a string not print a string. your shape __str__ functions must not be returning anything.
Yes, the critical thing is the return statement. It's natural to think that the function should print something, but in fact it doesn't print anything - it just returns a string. Use the code to build the string you want to return, and then at the end return whatever that string is.
That was exactly it, thanks bwCA and thinkpad20!
Join our real-time social learning platform and learn together with your friends!