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 许可协议
是这部分:
python 中的括号可能意味着不同的东西。您可以像在数学中那样使用它们来对表达式进行分组,就像您在体积和面积计算中所做的那样。但它们也用于函数调用,如
reportSphereVolume(radius)
。而且它们也不能用于乘法。相反,您必须使用明确的*
。math.pi
是一个float
常量,当它用括号这样写时,python 认为你正在尝试将它作为函数调用。因此错误:TypeError 'float' object is not callable'
。它应该是: