PYTHON PROGRAMMING Geometry Calculator Write a program that
PYTHON PROGRAMMING
Geometry Calculator
Write a program that displays the following menu:
Geometry Calculator
1. Calculate the Area of a Circle
2. Calculate the Area of a Rectangle
3. Calculate the Area of a Triangle
4. Quit
Enter your choice (1 - 4):
If the user enters 1, the program should ask for the radius of the circle and then display its area. Use 3.14159 for pi.
If the user enters 2, the program should ask for the length and width of the rectangle and then display the rectangle\'s area.
If the user enters 3, the program should ask for the length of the triangle\'s base and its height, and then display its area.
If the user enters 4, the program should end.
The user should be able to complete as many calculations as he/she wishes until a choice is made to quit.
Input Validation:
Do not accept values outside the range of 1 and 4 when selecting from the menu.
Do not accept negative values for any input amounts(ie. radius, length, width, etc).
Solution
def main():
#Declare and initialize variables
#string menu
gemMenu = \" \"
#Floats
radius = length = width = base = height = -1
circle_area = rectangle_area = triangle_area = 0
#Const Pi
Pi = 3.14159
#Display Intro
print(\"WELCOME TO THE GEOMETRY CALCULATOR PROGRAM!!\")
while gemMenu != \"4\":
#Display gemMenu
print(\"\ 1.) Calculate the Area of a Circle\ 2.) Calculate the Area of a Rectangle\ 3.) Calculate the Area of a Triangle\ 4.) Quit\ \")
#Prompt user for choice from gemMenu
gemMenu = input(\"Enter your choice from 1-4: \")
if gemMenu == \"1\":
#Prompt user for radius of circle
while radius < 0:
radius = eval(input(\"\ Please enter the radius of the circle: \"))
#Display Invalid, Radius can not be negative
if radius < 0:
print(\"\ Invalid, Radius can not be negative.\")
#Calulate the areaof circle
circle_area = Pi * radius ** 2
#Display results
print(\"\ The area of the circle is: %0.2f\" % circle_area)
elif gemMenu == \"2\":
#Prompt user for length of rectangle
while length and width < 0:
length = eval(input(\"Please enter the length of the rectangle: \"))
#Prompt user for width of rectangle
width = eval(input(\"Please enter the width of the rectangle: \"))
#Display Invalid, length and/or width can not be negative.
if length and width < 0:
print(\"\ Invalid, length and/or width can not be negative.\")
#Calculate the area
rectangle_area = length * width
#Display the results
print(\"\ The area of the rectangle is,\",rectangle_area)
elif gemMenu == \"3\":
while base and height < 0:
#Prompt user for base of triangle
base = eval(input(\"Please enter the length of the base of the triangle: \"))
#Prompt the user for height of triangle
height = eval(input(\"Please enter the height of the triangle: \"))
if base and height < 0:
#Display invalid, base and/or height can not be negative
print(\"\ Invalid, Base and/or Height can not be negative.\")
#Calculate area
triangle_area = 1/2 * base * height
#Display results
print(\"\ The area of the triangle is,\",triangle_area)
elif gemMenu == \"4\":
#End program message
print(\"Ending program, have a blessed day!\ \")
else:
#Display Invalid. Muse choose 1 – 4 from the menu above
print(\"Invalid, you must choose from 1-4 from the menu above.\ \")


