Python 减去浮点数
我将以下代码嵌入到类中。每当我运行 distToPoint 时,它都会给出错误“不支持的操作数类型 -: 'NoneType' 和 'float'” 我不知道为什么它会以 NoneType 返回,我该怎么办让减法起作用?
self 和 p 都应该是成对的。
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))
I have the following code embeded in a class.Whenever I run distToPoint it gives the error 'unsupported operand type(s) for -: 'NoneType' and 'float'' 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))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了比较,
返回
For sake of comparison,
returns
您应该检查发送给函数的
p
值,以便它具有浮点数的x
和y
。旧帖子(再想一想,我认为您没有尝试以这种方式使用
distToPoint
):distToPoint
不返回值,这可能是问题所在。You should check what value of
p
you are sending to the function, so that it has anx
andy
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.