Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # The geometry assignment
- # Use chooses can find the area, volume, or primeter of a shape
- # last edited by Mansur
- # imports
- import math
- # FUNCTION DEFININGS
- def intro():
- print( \
- """This is a geometry program.
- Select a shape, enter the values, and results will be shown""")
- # Menus
- def getMeasure():
- separate(True)
- return str(input(\
- """[0] - Quit\n
- [1] - Area
- [2] - Volume
- [3] - Perimeter \n
- [INPUT] > """)).lower()
- def getShapeA():
- separate(True)
- return str(input(\
- """[0] - Back\n
- [1] - Rectangle
- [2] - Rectanglular Prism
- [3] - Circle
- [4] - Sphere
- [5] - Triangle
- [6] - Cylinder
- [7] - Cone
- [8] - Hexagon\n
- [INPUT] > """)).lower()
- def getShapeV():
- separate(True)
- return str(input(\
- """[0] - Back\n
- [1] - Rectangular Prism
- [2] - Sphere
- [3] - Cylinder
- [4] - Cone\n
- [INPUT] > """)).lower()
- def getShapeP():
- separate(True)
- return str(input(\
- """[0] - Back\n
- [1] - Rectangle
- [2] - Circle
- [3] - Right-Angle Triangle
- [4] - Hexagon
- [INPUT] > """)).lower()
- # ================ Get variables
- # returns radius
- def calcRadius(radius = 0):
- while True:
- try:
- radius = float(input("\n[Radius] > "))
- break
- except:
- inIn()
- return radius
- # returns length
- def calcLength(length = 0):
- while True:
- try:
- length = float(input("\n[Length] > "))
- break
- except:
- inIn()
- return length
- # returns width
- def calcWidth(width = 0):
- while True:
- try:
- width = float(input("\n[Width] > "))
- break
- except:
- inIn()
- return width
- # returns height
- def calcHeight(height = 0):
- while True:
- try:
- height = float(input("\n[Height] > "))
- break
- except:
- inIn()
- return height
- # returns base
- def calcBase(base = 0):
- while True:
- try:
- base = float(input("\n[Base] > "))
- break
- except:
- inIn()
- return base
- # returns side lenght
- def calcSidelen(sidelen = 0):
- while True:
- try:
- sidelen = float(input("\n[Side Length] > "))
- break
- except:
- inIn()
- return sidelen
- # ===============================
- # Utility functions
- # displays the answer with given arguments
- def answer(measure, shape, result, unit = ""):
- separate()
- print("The", measure, "for the", shape, "is", result, "units", unit)
- input("\nPress the enter key to continue")
- # displays invalid input by default, can be changed using arguments
- def inIn(text = "Invalid Input\n"):
- print(text)
- # draws a line that separates
- def separate(newline = False):
- if newline == False:
- print("=============================================================================")
- else:
- print("=============================================================================\n")
- # Main program loop
- separate()
- intro()
- while True:
- measure = getMeasure()
- if measure in "1 area":
- shape = getShapeA()
- if shape in "1 rectangle":
- length = calcLength()
- width = calcWidth()
- answer("area", "rectangle", (length * width), "squared")
- elif shape in "2 rectangular prism":
- length = calcLength()
- width = calcWidth()
- height = calcHeight()
- answer("surface area", "rectangular prism", (2 * ((length * height) + (length + width) + (height * width))), "squared")
- elif shape in "3 circle":
- answer("area", "circle", ((calcRadius() ** 2) * math.pi), "squared")
- elif shape in "4 sphere":
- answer("surface area", "sphere", ((calcRadius() ** 2) * math.pi * 4), "squared")
- elif shape in "5 triangle":
- height = calcHeight
- base = calcBase()
- answer("area", "triangle", ((base * height) / 2), "squared")
- elif shape in "6 cylinder":
- height = calcHeight()
- radius = calcRadius()
- answer("surface area", "cylinder", ((math.pi * (radius ** 2) * 2) + ((math.pi * (2 * radius)) * height)), "squared")
- elif shape in "7 cone":
- height = calcHeight()
- radius = calcRadius()
- answer("surface area", "cone", (math.pi * radius * (math.sqrt((radius ** 2) + (height ** 2)))), "squared")
- elif shape in "8 hexagon":
- radius = calcRadius()
- sidelength = calcSidelen()
- answer("area", "hexagon", (6 * (0.5 * (sidelength * radius))), "squared")
- elif shape in "0 back":
- continue
- else:
- inIn()
- elif measure in "2 volume":
- shape = getShapeV()
- if shape in "1 rectangular prism":
- answer("volume", "rectangular prism", (calcLength() * calcWidth() * calcHeight()), "cubed")
- elif shape in "2 sphere":
- answer("volume", "sphere", ((4/3) * math.pi * (calcRadius() ** 3)), "cubed")
- elif shape in "3 cylinder":
- height = calcHeight()
- answer("volume", "cylinder", (height * (2 * math.pi * (calcRadius() ** 2))), "cubed")
- elif shape in "4 cone":
- height = calcHeight()
- answer("volume", "cone", ((1/3) * (math.pi * (calcRadius() ** 2) * height)), "cubed")
- elif shape in "0 back":
- continue
- else:
- inIn()
- elif measure in "3 perimeter":
- shape = getShapeP()
- if shape in "1 rectangle":
- length = calcLength()
- answer("perimeter", "rectangle", (2 * (length + calcWidth())))
- elif shape in "2 circle":
- answer("perimeter / circumference", "circle", (2 * calcRadius() * math.pi))
- elif shape in "3 right-angle triangle":
- height = calcHeight()
- base = calcBase()
- answer("perimeter", "right-angle triangle", (math.sqrt((height ** 2) + (base ** 2)) + height + base))
- elif shape in "4 hexagon":
- answer("perimeter", "hexagon", (calcSidelen() * 6))
- elif shape in "0 back":
- continue
- else:
- inIn()
- elif measure in "0 quit":
- break
- else:
- inIn()
- # Ending
- input("\n\nThanks for using this program")
- quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement