Exploring Functions
Thomas J. Kennedy
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…
-
Circle
-
Area
$$\pi r^{2}$$
- Perimeter
$$2 \pi r$$
-
-
Square
-
Area
$$s^2$$
- Perimeter
$$4 * s$$
-
-
Rectangle
-
Area
$$b * h$$
- Perimeter
$$2 * (b + h)$$
-
-
Right Triangle
-
Area
$$\frac{1}{2} * b * h$$
- Perimeter
$$b + h + \sqrt{b^2 + h^2}$$
-
We would like to have a practice problem generator where we can:
-
Enter the name of a shape.
-
Enter the dimensions (which vary based on shape).
-
Enter the number of decimal places to include in the answers (rounding).
-
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
-
Complete
def circle_perimeter(radius: float) -> float:
-
Complete
def circle_area(radius: float) -> float:
-
Complete
def square_perimeter(side: float) -> float:
-
Complete
def square_area(side: float) -> float:
-
Complete
def rectangle_perimeter(length: float, height: float) -> float:
-
Complete
def rectangle_area(length: float, height: float) -> float:
-
Complete
def right_triangle_perimeter(base: float, height: float) -> float:
-
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:
generate_problems.py
shapes.py
user_prompt.py
test/test_shapes.py
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…
- test 000 - code passes
test_perimeter_circle
- test 001 - code passes
test_area_circle
- test 002 - code passes
test_perimeter_square
- test 003 - code passes
test_area_square
- test 004 - code passes
test_perimeter_rectangle
- test 005 - code passes
test_area_rectangle
- test 006 - code passes
test_perimeter_right_triangle
- test 007 - code passes
test_area_right_triangle
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.