Python
Programming Exercises, Gently Explained
15
Special Cases and Gotchas
Length, width, and height can only be positive numbers. You could write additional code that
checks if any of these are negative numbers, and raise an exception in that case. However, this isn’t a
requirement for this exercise.
Now try to write a solution based on the information in the previous sections. If you still have
trouble solving this exercise,
read the Solution Template section for additional hints.
Solution Template
Try to first write a solution from scratch. But if you have difficulty, you can use the following
partial program as a starting place.
Copy the following code from https://invpy.com/areavolume-
template.py and paste it into your code editor. Replace the underscores with code to make a working
program:
def area(length, width):
# Return the product of the length and width:
return ____ * ____
def perimeter(length, width):
# Return the sum of the length twice and the width twice:
return ____ * 2 + width * ____
def volume(length, width, height):
# Return the product of the length, width, and height:
return ____ * ____ * ____
def surfaceArea(length, width, height):
# Return the sum of the area of each of the six sides:
return ((length * ____) + (length * ____) + (width * ____)) * 2
The complete solution for this exercise is given in Appendix A and
https://invpy.com/areavolume.py.
You can view each step of this program as it
runs under a debugger at https://invpy.com/areavolume-
debug/.