How do I take radius as a float argument and return the area/volume of a sphere back as floats in python?

Viewed 50

Why I have tried:

def sphere_area(radius: float):
    area = float(4 * math.pi * radius * radius)
    return area


def sphere_volume(radius: float):
    volume = float((4 / 3) * math.pi * math.pow(radius, 3))
    return volume

Expected output:

surface_area: #float number

volume: #float

1 Answers

If you want to output the area and volume of a sphere with a given radius, you need to call the functions you have defined.

For example, if you wanted the volume and surface area of a sphere with radius 2:

print(sphere_area(2))
print(sphere_volume(2))

We call the function name, put pass in our radius value, and print it out since we will only be returned a float value.

If you also wish, you can store the function values inside of two other variables:

sp_area = sphere_area(2)
sp_volume = sphere_area(2)

Remember to use import math at the top of your code so your code works when you use math functions.

I hope this helped! Let me know if you need any further clarifications or details :)

Related