I created a class that takes 2 coordinates and returns the slope and distance of the respective coordinates. It works fine but, apparently, what I did was wrong and was supposed to give an error.
My code:
class Line:
def __init__(self, coord1, coord2):
self.coord1 = coord1
self.coord2 = coord2
def distance(self):
return math.sqrt( ((self.coord1[0]-self.coord2[0])**2)+((self.coord1[1]-self.coord2[1])**2) )
def slope(self):
c = self.coord2[0]-self.coord1[0]
d = self.coord2[1]-self.coord1[1]
if d==0:
return "zero"
elif c==0:
return "Infinity"
else:
return d/c
line1=Line((8,3),(0,-4))
print(line1.distance())
print(line1.slope())
RESTART: C:\Users.....\Classes.py
10.63014581273465 **The result is right...?**
0.875
I get the right output, but this is how I am supposed to do it:
>>> line1=Line((8,3),(0,-4))
>>> line1.distance()
Traceback (most recent call last):File "<stdin>", line
1, in <module>TypeError: 'float' object is not callable **This is like mine but error?**
>>> line1.slope()
Traceback (most recent call last):File "<stdin>", line 1, in <module>TypeError:
'float' object is not callable **Just like mine but error?**
>>> line1.distance
10.63
>>> line1.slope
0.875
What is the difference and is mine wrong?