Reputation: 11
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
Upvotes: 0
Views: 57
Reputation: 43
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) + ")"
Upvotes: 1
Reputation: 99
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)
Upvotes: 0