如何解决 TypeError: 'float' object is not callable

新手上路,请多包涵
import math

def reportSphereVolume(r):
    SphereVolume = ((4/3)*math.pi*((r)**3))
    return SphereVolume

def reportSphereSurfaceArea(r):
    SphereSurfaceArea = ((4)*math.pi((r)**2))
    return SphereSurfaceArea

radius = int(input("What is the radius of the sphere? " ))
reportSphereVolume(radius)
reportSphereSurfaceArea(radius)

执行后我收到以下信息。

 What is the radius of the sphere? 10
Traceback (most recent call last):
  File "D:\Thonny\SphereAreaVolume.py", line 16, in <module>
    reportSphereSurfaceArea(radius)
  File "D:\Thonny\SphereAreaVolume.py", line 11, in reportSphereSurfaceArea
    SphereSurfaceArea = ((4)*math.pi((r)**2))
TypeError: 'float' object is not callable

我迷路了,我一直在看视频和阅读教科书,但我仍然无法解决。请帮忙。

原文由 Cam 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.5k
1 个回答

是这部分:

 math.pi((r)**2)

python 中的括号可能意味着不同的东西。您可以像在数学中那样使用它们来对表达式进行分组,就像您在体积和面积计算中所做的那样。但它们也用于函数调用,如 reportSphereVolume(radius) 。而且它们也不能用于乘法。相反,您必须使用明确的 *

math.pi 是一个 float 常量,当它用括号这样写时,python 认为你正在尝试将它作为函数调用。因此错误: TypeError 'float' object is not callable' 。它应该是:

 SphereSurfaceArea = (4)*math.pi*(r**2)

原文由 dennissv 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题