Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve 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)

When executed I receive the following.

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

I am lost, I have been watching videos and reading textbooks however I am still not able to solve. Please Help.

like image 695
Cam Avatar asked Sep 22 '18 03:09

Cam


Video Answer


2 Answers

It's this part:

math.pi((r)**2)

Parenthesis in python can mean different things. You can use them as you do in math to group expressions, like you've done in your volume and area calculations. But they're also used in function calls, as in reportSphereVolume(radius). And they also cannot be used for multiplication. Instead, you must use an explicit *.

math.pi is a float constant and when it's written like that with the parenthesis, python thinks that you're trying to call it as a function. Hence the error: TypeError 'float' object is not callable'. It should be:

SphereSurfaceArea = (4)*math.pi*(r**2)
like image 50
dennissv Avatar answered Oct 01 '22 00:10

dennissv


Notice the difference between the volume equation and the surface area equation (spaces added to make the difference more visually obvious):

SphereVolume =      ((4/3)*math.pi*((r)**3))
SphereSurfaceArea = ((4)  *math.pi ((r)**2))

The later is missing a multiplication operator (*) after math.pi. Unlike in algebra class, most programming languages require an explicit multiplication with the *. Parentheses after a name represent a function call in python.

I suggest that you use a little more caution with your parentheses. They should be used to make the purpose of a calculation more clear, for example when the order of operations aren't very obvious. In this case, some of your parentheses are extraneous and make the expression harder to read. For example, the parentheses around the entire expression and the ones around individual numbers or variables do not help clarify the order of operations and can be removed to make the expression more readable. On the other hand, the parentheses around the fraction (4/3) make sense as do the parentheses around the exponentiation (r**3) and (r**2) since these make it clear that you expect these operators to be evaluated first. With that in mind, you can do something like this:

SphereVolume =      (4/3)*math.pi*(r**3)
SphereSurfaceArea = 4*math.pi(r**2)

Notice how this is much easier to read and helps make the error a little easier to find.

like image 45
Code-Apprentice Avatar answered Sep 30 '22 22:09

Code-Apprentice