Exploring Functions

Thomas J. Kennedy

Contents:

1 The Problem

You probably recall the various area and perimeter formulae from Geometry. Suppose we want to generate a bunch of practice problems. Let us restrict ourselves to the following shapes and formulae…

We would like to have a practice problem generator where we can:

  1. Enter the name of a shape.

  2. Enter the dimensions (which vary based on shape).

  3. Enter the number of decimal places to include in the answers (rounding).

  4. Output the shape name, area, and perimeter.

1.1 Input

Input to this program will be interactive (i.e., a prompt will be displayed and the program will wait for keyboard input). User input will include the name of a shape and the corresponding dimensions.

1.2 Output

Suppose that a user wants to create a problem that centers around a circle…


---------------- Available Shapes ---------------- 1. Circle 2. Square 3. Rectangle 4. Right Triangle Select a shape: Circle Enter the radius: 2 Enter the number of decimal places: 2 A *Circle* with a radius *2* has what perimeter and area? - perimeter = 12.57 - area = 12.57 Would you like to generate another problem (yes/no)? no

2 Your Tasks

To complete this program… we need to make user of user input, conditional blocks, and loops. Since we have not reach those topics yet… that functionality has been provided. You task will be to write an area and perimeter function for each shape, specifically

  1. Complete

    def circle_perimeter(radius: float) -> float:
    

  2. Complete

    def circle_area(radius: float) -> float:
    
  3. Complete

    def square_perimeter(side: float) -> float:
    
  4. Complete

    def square_area(side: float) -> float:
    
  5. Complete

    def rectangle_perimeter(length: float, height: float) -> float:
    
  6. Complete

    def rectangle_area(length: float, height: float) -> float:
    
  7. Complete

    def right_triangle_perimeter(base: float, height: float) -> float:
    
  8. Complete

    def right_triangle_area(base: float, height: float) -> float:
    

You will notice that each function has a placeholder for pydoc documentation…

    """
    T.B.W.
    """

Completing the pydoc documentation is optional.

3 Mechanics

You can run the main program with…

python3.11 generate_problems.py

The test code can be run with…

python3.11 -m pytest -v tests

4 Files

There are four files in this assignment:

You will be working in shapes.py. Do not modify the other files.

You can access the starting files on GitHub. You may either clone the starting repository (through Git) or download a zip file.

5 Grading

Your grade will be based on the following criteria…

6 Submitting

You will submit your completed copy of shapes.py as a file upload through Canvas.

Do not alter any of the other source code files, nor change the module interface.