Assigned:Friday, September 23
Due: Sunday, October 2, 2016 by 11:55pm
For this assignment, you will write a program that allows the user to compute measurements of various geometric shapes using a menu-driven interface. This assignment is meant to test your ability to define and call functions properly, and it will also incorporate conditional statements (if-elif-else) as well as a simple while loop.
When first thinking about a program, try to envision it as a set of components that all fit together like a puzzle, where each component handles a separate, distinct task. Consider making separate tasks into separate functions. Now this idea can be taken to the extreme, which is not helpful, as using too many functions, especially if these functions all need access to the same variables, ends up being very messy because you have to write functions that take, for instance, ten arguments. Try to find a balance between too many functions and too few.
First, write five functions that compute and return the following:
This idea, while probably taken a little too far in this case, is normally good programming practice: We are using an already-solved problem to solve a new problem. Here, a square is just a special case of a rectangle, so use the solution for a rectangle we've already written rather than writing a completely new solution.
Note that the surface area of this shape can be computed as the sum of the areas of the six faces of the prism. Because each face is a rectangle, call your area of a rectangle function to write this function. You will need to call it three times.
Again, I know I'm taking the idea of reusing functions to the extreme, but I want you to practice calling functions and capturing return values.
Second, write your main() function to do this:
You may assume the user will type in the name of the shape either in all lowercase, or with a capital first letter (i.e., your program should work correctly for both "triangle" and "Triangle"). You do not have to handle any other type of capitalization. If the user types in an invalid shape name (like "TrIaNgLe" or "trapezoid"), your program should print an appropriate error message.
What shape do you want? triangle What is the base? 3 What is the height? 4 The area of the triangle is 6.0 Do you want to calculate another area? (yes or no) yes What shape do you want? Square What is the side length? 7 The area of the square is 49 Do you want to calculate another area? (yes or no) yes What shape do you want? rectangle What is the length? 8 What is the width? 5 The area of the rectangle is 40 Do you want to calculate another area? (yes or no) yes What shape do you want? prism What is the length? 6 What is the width? 5 What is the height? 4 The surface area of the prism is 148 Do you want to calculate another area? (yes or no) yes What shape do you want? Hexagon I don't know that shape. Do you want to calculate another area? (yes or no) yes What shape do you want? square What is the side length? 5 The area of the square is 25 Do you want to calculate another area? (yes or no) yes What shape do you want? Triangle What is the base? 9 What is the height? 8 The area of the triangle is 36.0 Do you want to calculate another area? (yes or no) yes What shape do you want? Circle What is the radius? 6 The area of the circle is 113.03999999999999 Do you want to calculate another area? (yes or no) no
Comments in your code
Because I'm requiring you to write functions in this assignment, I'm adding additional mandatory comments. It is good practice to always describe your functions in terms of what they do, the input they take in and the output they produce.
Here's an example you'd use for a function that computes the circumference of a circle:
# This function computes the circumference of a circle. # Parameters: r, the radius of the circle. # Returns: the circumference of the circle. def circumference_of_circle(r): return 2 * math.pi * rWhat to Do
Requirements
A good program will do all of the following:
Your program will be graded on correctness, as well as on coding style, which refers to choices you make when writing your code, such as good use of variable names, appropriate indentation,
and comments (this is not an exhaustive list). See the syllabus for further grading details.
You will receive one bonus point for every complete day your program is turned in early, up to a maximum of five points. For instance, if your program is due on September 20 at 11:59pm, if you turn in your code on Moodle any time on September 19 from 12:00am through 11:59pm, you will receive one extra point on your project. Programs submitted on September 18 from 12:00am through 11:59pm will receive two points. This pattern continues for up to five points.