如何解决TypeError:'float'对象不可调用

Cam*_*Cam 4 python python-3.x

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)
Run Code Online (Sandbox Code Playgroud)

执行时,我收到以下内容.

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
Run Code Online (Sandbox Code Playgroud)

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

den*_*ssv 6

这是这部分:

math.pi((r)**2)
Run Code Online (Sandbox Code Playgroud)

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

math.pi是一个float常量,当它用括号写成时,python认为你试图把它称为函数.因此错误:TypeError 'float' object is not callable'.它应该是:

SphereSurfaceArea = (4)*math.pi*(r**2)
Run Code Online (Sandbox Code Playgroud)

  • 您应该注意python语法和数学符号之间的区别.这可能是OP混乱的根源. (2认同)