Python Forum
Please help! Problem with my Point object
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help! Problem with my Point object
#1
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....
Reply


Messages In This Thread
Please help! Problem with my Point object - by itrema - Mar-05-2019, 09:20 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb Object Oriented programming (OOP) problem OmegaRed94 6 2,918 May-31-2021, 07:19 PM
Last Post: OmegaRed94
  connecting the first point to the last point Matplotlib omar_mohsen 0 4,590 Jan-15-2020, 01:23 PM
Last Post: omar_mohsen
  problem with "hiding" object league55 4 3,203 Jan-16-2018, 11:21 PM
Last Post: league55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020