Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Please help! Problem with my Point object itrema Unladen Swallow Posts: 2 Threads: 1 Joined: Mar 2019 Reputation: 0 Likes received: 0 #1 Mar-05-2019, 09:20 AM Hello everyone! I am having a problem with my code and after minutes of search, I landed here. I need your help. Here is the problem: We want to create a Point object capable of handling the most basic vector operations; we want our Point object to handle addition and subtraction. For two points (x1,y1)+(x2,y2)=(x1+x2,y1+y2) and similarly for subtraction. Implement a method within Point that allows two Point objects to be added together using the + operator, and likewise for subtraction. Here is my code: ```class Point(object): def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return "Point({0}, {1})".format(self.x, self.y) def __add__(self, other): a = self.x + other.x b = self.y + other.y return "Point({a}, {b})" def __sub__(a, b): c = self.x - other.x d = self.y - other.y return "Point({c}, {d})" ```And I've got type erreor. Can anyone help me? By the way if you have an idea for multiplication, I'll be.... DeaD_EyE Giant Foot Posts: 951 Threads: 5 Joined: May 2017 Reputation: 81 Likes received: 197 #2 Mar-05-2019, 09:45 AM ```class Point(object): __slots__ = ['x', 'y'] def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return "Point({0}, {1})".format(self.x, self.y) def __add__(self, other): x, y = self.x + other.x, self.y + other.y return self.__class__(x, y) def __sub__(self, other): x, y = self.x - other.x, self.y - other.y return self.__class__(x, y) ``` added __slots__ to save memory of the crated object from class method signature of sub was wrong A Point should be immutable. Doing an operation with two points, should return a new point. Instead of `self.__class__`, you can use also direct the name `Point`. The first one is good for inheritance. For training, you could add other methods. For example a conversion to polar coordinates: https://en.wikipedia.org/wiki/Polar_coor...oordinates You can use a new class for this different representation or you do it inside of the `Point` class, maybe as a property. My code examples are always for Python >=3.6.0 Almost dead, but too lazy to die: https://sourceserver.info All humans together. We don't need politicians! itrema Unladen Swallow Posts: 2 Threads: 1 Joined: Mar 2019 Reputation: 0 Likes received: 0 #3 Mar-05-2019, 09:57 AM Thank you very much for your help. It worked « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Finding nearest point of a Multidigraph in Python 3.7 stixmagiggins 3 110 Aug-16-2019, 01:32 PM Last Post: ThomasL Song Modifying Program Starting Point BadenJaden 0 88 Jul-30-2019, 11:28 PM Last Post: BadenJaden Pygal: Displaying information for each data point KirkmanJ 0 73 Jul-29-2019, 01:10 PM Last Post: KirkmanJ Practicing using a "flag": please point in right direction magsloo 5 295 May-10-2019, 04:58 AM Last Post: perfringo decimal point or comma Skaperen 8 468 Mar-17-2019, 06:02 AM Last Post: DeaD_EyE mixed decimal point characters Skaperen 1 217 Mar-10-2019, 05:23 PM Last Post: stullis Getting the maximum point of the graph from the text file Erfans 2 323 Mar-09-2019, 11:01 PM Last Post: Erfans Subtracting values between two dictionaries/ floating point numbers FloppyPoppy 5 619 Mar-04-2019, 01:00 PM Last Post: snippsat Conversion needed from bytearray to Floating point braveYug 1 542 May-07-2018, 12:23 PM Last Post: snippsat Add parameter to math.floor() to round to specific decimal point gabrield 2 620 Mar-09-2018, 08:43 PM Last Post: wavic

Forum Jump:

Users browsing this thread: 1 Guest(s)