求解Python中TypeError: 'float' object is not callable错误
解决TypeError: 'float' object is not callable的问题
嘿,我一眼就揪出问题所在啦!你这段代码里的distance计算行犯了个典型的语法逻辑错误:
distance = ((6371*math.pi)/180)(math.sqrt((-35.276159-latitude)**2+(149.120893-longitude)**2))
这里((6371*math.pi)/180)会先算出一个浮点数结果,可你紧接着用括号把math.sqrt(...)包了起来——这在Python里相当于把这个浮点数当成函数来调用,可浮点数根本不是函数呀,自然就触发TypeError: 'float' object is not callable报错了!
你需要用乘法运算符*把这两部分明确连接起来(Python里不能像手写数学公式那样省略乘号),修正后的代码应该是这样:
import math # 别忘了确保导入了math模块 for data in list: latitude = float(data[1]) longitude = float(data[2]) # 将括号替换为明确的乘号* distance = ((6371*math.pi)/180) * math.sqrt((-35.276159-latitude)**2 + (149.120893-longitude)**2) print(distance)
顺带说一句:你打印latitude和longitude的类型是float完全没问题,这俩变量不是报错的原因,纯粹是计算行的语法逻辑出错啦~
内容的提问来源于stack exchange,提问作者Tom Lewis
相关产品推荐
相关产品推荐

