Assigned:Friday, September 20
Due: Sunday, September 29, 2019 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 cube is just a special case of a rectangular prism, so use the solution for a rectangular prism we've already written rather than writing a completely new solution.
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.
Note: You may import math at the top of your program and use math.pi in your volume calculations. You should get the same solutions as below if you do.
You may instead use 3.14 as the value of pi in your code, in which case your sphere, cone and ice cream volumes will be slightly different than the solutions below.
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 "sphere" and "Sphere"). You do not have to handle any other type of capitalization. If the user types in an invalid shape name (like "SpHeRe" or "trapezoid"), your program should print an appropriate error message.
What shape do you want? sphere What is the radius? 5 The volume of the sphere is 523.5987755982989 Would you like to calculate another volume?(yes or no) yes What shape do you want? cube What is the side length? 4 The volume of the cube is 64.0 Would you like to calculate another volume?(yes or no) yes What shape do you want? ice cream What is the radius? 3 What is the height? 7 The volume of the ice cream cone is 122.52211349000193 Would you like to calculate another volume?(yes or no) yes What shape do you want? Prism What is the length? 3 What is the width? 4 What is the height? 5 The volume of the rectangular prism is 60.0 Would you like to calculate another volume?(yes or no) yes What shape do you want? Triangle I don't know that shape. Would you like to calculate another volume?(yes or no) yes What shape do you want? Cone What is the radius? 6 What is the height? 8 The volume of the cone is 301.59289474462014 Would you like to calculate another volume?(yes or no) yes What shape do you want? hexagon I don't know that shape. Would you like to calculate another volume?(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.