Advertisement
Helium

Project - Geometry

Sep 17th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.38 KB | None | 0 0
  1. # The geometry assignment
  2. # Use chooses can find the area, volume, or primeter of a shape
  3. # last edited by Mansur
  4.  
  5. # imports
  6. import math
  7.  
  8.  
  9. # FUNCTION DEFININGS
  10. def intro():
  11.     print( \
  12.           """This is a geometry program.
  13. Select a shape, enter the values, and results will be shown""")
  14.  
  15. # Menus
  16. def getMeasure():
  17.     separate(True)
  18.     return str(input(\
  19.         """[0] - Quit\n
  20. [1] - Area
  21. [2] - Volume
  22. [3] - Perimeter \n
  23. [INPUT] > """)).lower()
  24.  
  25. def getShapeA():
  26.     separate(True)
  27.     return str(input(\
  28.         """[0] - Back\n
  29. [1] - Rectangle
  30. [2] - Rectanglular Prism
  31. [3] - Circle
  32. [4] - Sphere
  33. [5] - Triangle
  34. [6] - Cylinder
  35. [7] - Cone
  36. [8] - Hexagon\n
  37. [INPUT] > """)).lower()
  38.  
  39. def getShapeV():
  40.     separate(True)
  41.     return str(input(\
  42.         """[0] - Back\n
  43. [1] - Rectangular Prism
  44. [2] - Sphere
  45. [3] - Cylinder
  46. [4] - Cone\n
  47. [INPUT] > """)).lower()
  48.  
  49. def getShapeP():
  50.     separate(True)
  51.     return str(input(\
  52.         """[0] - Back\n
  53. [1] - Rectangle
  54. [2] - Circle
  55. [3] - Right-Angle Triangle
  56. [4] - Hexagon
  57. [INPUT] > """)).lower()
  58.  
  59. # ================ Get variables
  60. # returns radius
  61. def calcRadius(radius = 0):
  62.     while True:
  63.         try:
  64.             radius = float(input("\n[Radius] > "))
  65.             break
  66.         except:
  67.             inIn()
  68.     return radius  
  69. # returns length
  70. def calcLength(length = 0):
  71.     while True:
  72.         try:
  73.             length = float(input("\n[Length] > "))
  74.             break
  75.         except:
  76.             inIn()
  77.     return length
  78. # returns width
  79. def calcWidth(width = 0):
  80.     while True:
  81.         try:
  82.             width = float(input("\n[Width] > "))
  83.             break
  84.         except:
  85.             inIn()
  86.     return width
  87. # returns height
  88. def calcHeight(height = 0):
  89.     while True:
  90.         try:
  91.             height = float(input("\n[Height] > "))
  92.             break
  93.         except:
  94.             inIn()
  95.     return height
  96. # returns base
  97. def calcBase(base = 0):
  98.     while True:
  99.         try:
  100.             base = float(input("\n[Base] > "))
  101.             break
  102.         except:
  103.             inIn()
  104.     return base
  105. # returns side lenght
  106. def calcSidelen(sidelen = 0):
  107.     while True:
  108.         try:
  109.             sidelen = float(input("\n[Side Length] > "))
  110.             break
  111.         except:
  112.             inIn()
  113.     return sidelen
  114.  
  115. # ===============================
  116.  
  117. # Utility functions
  118. # displays the answer with given arguments
  119. def answer(measure, shape, result, unit = ""):
  120.     separate()
  121.     print("The", measure, "for the", shape, "is", result, "units", unit)
  122.     input("\nPress the enter key to continue")
  123.  
  124. # displays invalid input by default, can be changed using arguments
  125. def inIn(text = "Invalid Input\n"):
  126.     print(text)
  127.  
  128. # draws a line that separates
  129. def separate(newline = False):
  130.     if newline == False:
  131.         print("=============================================================================")
  132.     else:
  133.         print("=============================================================================\n")
  134.  
  135. # Main program loop
  136. separate()
  137. intro()
  138. while True:
  139.     measure = getMeasure()
  140.     if measure in "1 area":
  141.         shape = getShapeA()
  142.         if shape in "1 rectangle":
  143.             length = calcLength()
  144.             width = calcWidth()
  145.             answer("area", "rectangle", (length * width), "squared")
  146.         elif shape in "2 rectangular prism":
  147.             length = calcLength()
  148.             width = calcWidth()
  149.             height = calcHeight()
  150.             answer("surface area", "rectangular prism", (2 * ((length * height) + (length + width) + (height * width))), "squared")
  151.         elif shape in "3 circle":
  152.             answer("area", "circle", ((calcRadius() ** 2) * math.pi), "squared")
  153.         elif shape in "4 sphere":
  154.             answer("surface area", "sphere", ((calcRadius() ** 2) * math.pi * 4), "squared")
  155.         elif shape in "5 triangle":
  156.             height = calcHeight
  157.             base = calcBase()
  158.             answer("area", "triangle", ((base * height) / 2), "squared")
  159.         elif shape in "6 cylinder":
  160.             height = calcHeight()
  161.             radius = calcRadius()
  162.             answer("surface area", "cylinder", ((math.pi * (radius ** 2) * 2) + ((math.pi * (2 * radius)) * height)), "squared")
  163.         elif shape in "7 cone":
  164.             height = calcHeight()
  165.             radius = calcRadius()
  166.             answer("surface area", "cone", (math.pi * radius * (math.sqrt((radius ** 2) + (height ** 2)))), "squared")
  167.         elif shape in "8 hexagon":
  168.             radius = calcRadius()
  169.             sidelength = calcSidelen()
  170.             answer("area", "hexagon", (6 * (0.5 * (sidelength * radius))), "squared")
  171.         elif shape in "0 back":
  172.             continue
  173.         else:
  174.             inIn()
  175.     elif measure in "2 volume":
  176.         shape = getShapeV()
  177.         if shape in "1 rectangular prism":
  178.             answer("volume", "rectangular prism", (calcLength() * calcWidth() * calcHeight()), "cubed")
  179.         elif shape in "2 sphere":
  180.             answer("volume", "sphere", ((4/3) * math.pi * (calcRadius() ** 3)), "cubed")
  181.         elif shape in "3 cylinder":
  182.             height = calcHeight()
  183.             answer("volume", "cylinder", (height * (2 * math.pi * (calcRadius() ** 2))), "cubed")
  184.         elif shape in "4 cone":
  185.             height = calcHeight()
  186.             answer("volume", "cone", ((1/3) * (math.pi * (calcRadius() ** 2) * height)), "cubed")
  187.         elif shape in "0 back":
  188.             continue
  189.         else:
  190.             inIn()
  191.     elif measure in "3 perimeter":
  192.         shape = getShapeP()
  193.         if shape in "1 rectangle":
  194.             length = calcLength()
  195.             answer("perimeter", "rectangle", (2 * (length + calcWidth())))
  196.         elif shape in "2 circle":
  197.             answer("perimeter / circumference", "circle", (2 * calcRadius() * math.pi))
  198.         elif shape in "3 right-angle triangle":
  199.             height = calcHeight()
  200.             base = calcBase()
  201.             answer("perimeter", "right-angle triangle", (math.sqrt((height ** 2) + (base ** 2)) + height + base))
  202.         elif shape in "4 hexagon":
  203.             answer("perimeter", "hexagon", (calcSidelen() * 6))
  204.         elif shape in "0 back":
  205.             continue
  206.         else:
  207.             inIn()
  208.     elif measure in "0 quit":
  209.         break
  210.     else:
  211.         inIn()
  212.  
  213. # Ending
  214. input("\n\nThanks for using this program")
  215. quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement