Python subtracting floats
I have the following code embeded in a class.Whenever I run distToPoint it gives the error 'unsupported operand type(s) for -: 'NoneType' and 'f开发者_JAVA技巧loat'' I don't know why it's returning with NoneType and how do I get the subtraction to work?
Both self and p are supposed to be pairs.
def __init__(self, x, y):
self.x = float(x)
self.y = float(y)
def distToPoint(self,p):
self.ax = self.x - p.x
self.ay = self.y - p.y
self.ac = math.sqrt(pow(self.ax,2)+pow(self.ay,2))
For sake of comparison,
import math
class Point(object):
def __init__(self, x, y):
self.x = x + 0.
self.y = y + 0.
def distToPoint(self, p):
dx = self.x - p.x
dy = self.y - p.y
return math.sqrt(dx*dx + dy*dy)
a = Point(0, 0)
b = Point(3, 4)
print a.distToPoint(b)
returns
5.0
You should check what value of p
you are sending to the function, so that it has an x
and y
that are floats.
Old post (on second thought, I don't think you were trying to use distToPoint
this way):
distToPoint
doesn't return a value, this is probably the problem.
精彩评论