CS 115 Lab 8, Part C: Refactor: geometric calculations

[Back to lab instructions]


Instructions

  1. Create and open a new Python source code file named lab08c.py:
    """
    Program: CS 115 Lab 8c
    Author: Your name
    Description: This program computes geometric quantities.
    """
    import sys
    import math
    
    
    def get_numeric_val():
        """
        Ask the user for a number
        Exit if the user does not supply a positive value.
        Otherwise, return the value they entered
        """
    
        num = float(input('Enter a positive numeric value: '))
        if num <= 0:
            sys.exit('Error: that number was not positive.')
        return num
    
    
    def get_menu_choice():
        """ Print the menu and return the user's selection """
        pass
    
    
    def compute_square_area(side):
        """
        Compute and return the area of a square.
        Parameter: side length of the square
        """
        pass
    
    
    def compute_circle_area(radius):
        """
        Compute and return the area of a circle.
        Parameter: radius of the circle
        """
        pass
    
    
    def compute_cube_volume(edge):
        """
        Compute and return the volume of a cube.
        Parameter: edge length of the cube
        """
        pass
    
    
    def main():
    
        menu_choice = get_menu_choice()  # Get the user's first choice
    
        while menu_choice != 'q':
            user_num = get_numeric_val()  # Get the side length (etc.)
    
            if menu_choice == 'a':
                print('The area of a square with side length ',
                      user_num, ' is ', round(compute_square_area(user_num), 5),
                      '.', sep="")
    
            elif menu_choice == 'b':
                print('The area of a circle with radius length ',
                      user_num, ' is ', round(compute_circle_area(user_num), 5),
                      '.', sep="")
    
            elif menu_choice == 'c':
                print('The volume of a cube with edge length ',
                      user_num, ' is ', round(compute_cube_volume(user_num), 5),
                      '.', sep="")
    
            menu_choice = get_menu_choice()  # Get user's next choice
    
    
    main()
    
  2. Answer Question 6 in your writeup.
  3. Right now, some of your functions have empty definitions (which is what pass means). Replace the definition of get_menu_choice() with code to do the following:
  4. Fill in the function compute_square_area so that it returns the area of a square whose side length is side. It is possible to write this function in only one line of code (not counting the def line).
  5. Run your program. Here is a sample input/output sequence:
    Would you like to
    a. Calculate the area of a square?
    b. Calculate the area of a circle?
    c. Calculate the volume of a cube?
    d. Calculate the volume of a sphere?
    e. Calculate the area of an equilateral triangle?
    q. Quit?
    A
    Enter a numeric value: 5
    The area of a square with side length 5.0 is 25.0.
    Would you like to
    a. Calculate the area of a square?
    b. Calculate the area of a circle?
    c. Calculate the volume of a cube?
    d. Calculate the volume of a sphere?
    e. Calculate the area of an equilateral triangle?
    q. Quit?
    q
  6. Fill in the function compute_circle_area so that it returns the area of a circle whose radius is radius.
  7. Fill in the function compute_cube_volume so that it returns the volume of a cube whose edge length is edge.
  8. Define a function compute_sphere_volume that computes the volume of a sphere. Your function should take one parameter, radius, and return the volume of a sphere with that radius. Write a docstring at the beginning of your function explaining what it does.
  9. Define a function compute_tri_area that computes the area of an equilateral triangle. Your function should take one parameter, side, and return the area of an equilateral triangle with that side length. Write a docstring at the beginning of your function explaining what it does.
  10. Add code to the main function to call your new functions and print their output when the user selects 'd' or 'e'. You should round these outputs to 5 decimal places.
  11. Test your program thoroughly. Here is a sample input/output sequence:
    Would you like to
    a. Calculate the area of a square?
    b. Calculate the area of a circle?
    c. Calculate the volume of a cube?
    d. Calculate the volume of a sphere?
    e. Calculate the area of an equilateral triangle?
    q. Quit?
    c
    Enter a numeric value: 2
    The volume of a cube with edge length 2.0 is 8.0.
    Would you like to
    a. Calculate the area of a square?
    b. Calculate the area of a circle?
    c. Calculate the volume of a cube?
    d. Calculate the volume of a sphere?
    e. Calculate the area of an equilateral triangle?
    q. Quit?
    d
    Enter a positive numeric value: 5
    The volume of a sphere with radius length 5.0 is 523.59878.
    Would you like to
    a. Calculate the area of a square?
    b. Calculate the area of a circle?
    c. Calculate the volume of a cube?
    d. Calculate the volume of a sphere?
    e. Calculate the area of an equilateral triangle?
    q. Quit?
    e
    Enter a positive numeric value: 12.2
    The area of an equilateral triangle with side length 12.2 is 64.44961.
    Would you like to
    a. Calculate the area of a square?
    b. Calculate the area of a circle?
    c. Calculate the volume of a cube?
    d. Calculate the volume of a sphere?
    e. Calculate the area of an equilateral triangle?
    q. Quit?
    q
  12. When you are convinced that your program is working correctly, demo it for an instructor.
  13. Continue to Part D.