0

This is a method overload

    class Point():
        def __init__(self, x=0, y=0):
            self.x = x
            self.y = y
            self.coords = (self.x, self.y)


        def move(self, x, y):
            self.x += x
            self.y += y


        def __add__(self, p):
            return Point(self.x + p.x, self.y + p.y)

        def __sub__(self, p):
            return Point(self.x - p.x, self.y - p.y)

        def __mul__(self, p):
            return (self.x * p.x + self.y * p.y)




        def __str__(self):  
            return  str(self.x)  str(self.y)

    p1 = Point(3,4)
    p2 = Point(3,2)
    p3 = Point(1,3)
    p4 = Point(0,1)
    p5 = p1 + p2
    p6 = p4 - p1
    p7 = p2*p3

    print(p5, p6, p7)

I keep getting this error. Can someone explain why this is the incorrect syntax

return str(self.x) str(self.y) ^ SyntaxError: invalid syntax

2 Answers 2

1

This will not return a string

return  str(self.x),  str(self.y)

You should return it as a string

return  "(" + str(self.x) + ", " + str(self.y) + ")"
0

Invalid syntax is because you are trying to return 2 variables you need to comma separate them.

    def __str__(self):  
        return  str(self.x),  str(self.y)
3
  • When I put a comma between the two variables I get this error
    – Alex
    Commented Oct 30, 2020 at 1:14
  • print(p5, p6, p7) TypeError: str returned non-string (type tuple)
    – Alex
    Commented Oct 30, 2020 at 1:14
  • Also why can't I return variables.
    – Alex
    Commented Oct 30, 2020 at 1:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.