Volume Calculator

Learning Objectives

By completing this challenge we are going to learn how to define our own functions in Python.
We will also assign parameters to our functions.

Look at the code below used to calculate the volume of a cube. This code is based on the use of a function called getVolumeOfCube(). This function takes one parameter (width) and return the volume of the cube (in mL).

def getVolumeOfCube(width):
    volume = width * width * width
    #This calculates the volume in cubic millimeters. Divide it by 1000 to get this volume in mL
    volume=volume/1000
    return volume

#Main Program Starts Here
cubeVolume=getVolumeOfCube(50)
print("The volume of a cube with a side of 50mm is: " + str(cubeVolume) + " mL.")

Challenge #1:


Complete the following code to add 3 functions as follows:

  • getVolumeOfCylinder(radius, height)
  • getVolumeOfCuboid(width, length, height)
  • getVolumeOfCone(radius, height)

Challenge #2:


Use all of your functions to calculate the volume of each of these bottles:
BottlesABCD
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 4 / 5. Vote count: 26

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!