Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1. print "This is to find the area or perimeter of a rectangle or triangle?"
  2. print "Do you want to find the area or perimeter of a rectangle(r) or triangle(t)?"
  3. l= raw_input("I want to find the area or perimeter of a ")
  4. if l=='r':
  5. print "Do you want to find the area(a) or perimeter(p) of your rectangle?"
  6. a= raw_input(" I want to find the ")
  7. if a=='p':
  8. print "What is the length of the rectangle?"
  9. b = int(raw_input("The length of the rectangle is "))
  10. print "What is the width of the rectangle?"
  11. c = int(raw_input("THe width of the rectangle is "))
  12. d = (2 * b) + (2 * c)
  13. print d
  14. elif a=='a':
  15. print "Got it. What is the length of your rectangle?"
  16. x = int(raw_input("The length of the rectangle is "))
  17. print "What is the width of your rectangle?"
  18. y = int(raw_input("The width of the rectangle is "))
  19. z = x * y
  20. print z
  21. if l=='t':
  22. print "Do you want to find the area(a) or perimeter(p) of your triangle?"
  23. m= raw_input("I want to find the "
  24. if m == 'a':
  25. print "What is the length of your triangle?"
  26. n = int(raw_input("The length of the triangle is "
  27. print "What is the width of your triangle?
  28. o = int(raw_input("The width of the triagle is "
  29. q = (n * o)/2
  30. print q
  31. elif m=='p'
  32. print "What is the first length of your triangle?"
  33. r = int(raw_input("The first length of the triangle is ")
  34. print "What is the second length of the triangle?"
  35. s = int(raw_input("The second length of the triangle is ")
  36. print "What is the third length of the triangle?"
  37. u = int(raw_input(" The third length of the triangle is ")
  38. v = r + s + u
  39. print v
  40.  
  41. print "This is to find the area or perimeter of a rectangle or triangle?"
  42. print "Do you want to find the area or perimeter of a rectangle(r) or triangle(t)?"
  43. l = raw_input("I want to find the area or perimeter of a ")
  44. if l == 'r':
  45. print "Do you want to find the area(a) or perimeter(p) of your rectangle?"
  46. a = raw_input(" I want to find the ")
  47. if a == 'p':
  48. print "What is the length of the rectangle?"
  49. b = int(raw_input("The length of the rectangle is "))
  50. print "What is the width of the rectangle?"
  51. c = int(raw_input("THe width of the rectangle is "))
  52. d = (2 * b) + (2 * c)
  53. print d
  54. elif a == 'a':
  55. print "Got it. What is the length of your rectangle?"
  56. x = int(raw_input("The length of the rectangle is "))
  57. print "What is the width of your rectangle?"
  58. y = int(raw_input("The width of the rectangle is "))
  59. z = x * y
  60. print z
  61. if l == 't':
  62. print "Do you want to find the area(a) or perimeter(p) of your triangle?"
  63. m = raw_input("I want to find the ")
  64. if m == 'a':
  65. print "What is the length of your triangle?"
  66. n = int(raw_input("The length of the triangle is "))
  67. print "What is the width of your triangle?"
  68. o = int(raw_input("The width of the triagle is "))
  69. q = (n * o) / 2
  70. print q
  71. elif m == 'p':
  72. print "What is the first length of your triangle?"
  73. r = int(raw_input("The first length of the triangle is "))
  74. print "What is the second length of the triangle?"
  75. s = int(raw_input("The second length of the triangle is "))
  76. print "What is the third length of the triangle?"
  77. u = int(raw_input(" The third length of the triangle is "))
  78. v = r + s + u
  79. print v
  80.  
  81. m = raw_input("I want to find the "
  82.  
  83. m = raw_input("I want to find the ")
  84.  
  85. from math import pi
  86. import sys
  87.  
  88. # version compatibility shim
  89. if sys.hexversion < 0x3000000:
  90. # Python 2.x
  91. inp = raw_input
  92. else:
  93. # Python 3.x
  94. inp = input
  95.  
  96. #
  97. # utility functions
  98. #
  99.  
  100. def type_getter(datatype):
  101. def get_type(prompt):
  102. while True:
  103. try:
  104. return datatype(inp(prompt))
  105. except ValueError:
  106. pass
  107. return get_type
  108.  
  109. get_float = type_getter(float)
  110. get_int = type_getter(int)
  111.  
  112. def do_menu(prompt, choices):
  113. print("")
  114. # make the display value 1-based
  115. for i,choice in enumerate(choices, 1):
  116. print("{:>3}: {}".format(i, choice))
  117. # make the return value 0-based
  118. return get_int(prompt + "[1-{}] ".format(len(choices))) - 1
  119.  
  120. #
  121. # base class
  122. #
  123.  
  124. class Shape(object):
  125. name = None # subclass will override
  126. fields = [] # these values
  127.  
  128. def __init__(self, **kwargs):
  129. super(Shape, self).__init__() # <= no longer needed in Python 3
  130.  
  131. for field in self.fields:
  132. if field in kwargs:
  133. value = kwargs[field]
  134. else:
  135. value = get_float("What is the {}'s {}? ".format(self.name, field))
  136. setattr(self, field, value)
  137.  
  138. def area(self):
  139. raise NotImplemented
  140.  
  141. def perimeter(self):
  142. raise NotImplemented
  143.  
  144. #
  145. # Subclasses
  146. #
  147.  
  148. class Triangle(Shape):
  149. name = "triangle"
  150. fields = ["width", "height"]
  151.  
  152. def area(self):
  153. return 0.5 * self.width * self.height
  154.  
  155. def perimeter(self):
  156. # We will assume the width and height are
  157. # adjacent and opposite sides of a right triangle
  158. adj, opp = self.width, self.height
  159. hyp = (adj*adj + opp*opp)**0.5
  160. return adj + opp + hyp
  161.  
  162. class Rectangle(Shape):
  163. name = "rectangle"
  164. fields = ["width", "height"]
  165.  
  166. def area(self):
  167. return self.width * self.height
  168.  
  169. def perimeter(self):
  170. return 2 * (self.width + self.height)
  171.  
  172. class Ellipse(Shape):
  173. name = "ellipse"
  174. fields = ["long_axis", "short_axis"]
  175.  
  176. def area(self):
  177. return pi * self.long_axis * self.short_axis * 0.25
  178.  
  179. def perimeter(self):
  180. # by Ramanujan's approximation - see
  181. # http://www.mathsisfun.com/geometry/ellipse-perimeter.html
  182. ra = 0.5 * self.long_axis
  183. rb = 0.5 * self.short_axis
  184. return pi * (3 * (ra + rb) - ((3 * ra + rb) * (ra + 3 * rb))**0.5)
  185.  
  186. known_shapes = [Triangle, Rectangle, Ellipse]
  187. known_actions = ["area", "perimeter"]
  188.  
  189. def main():
  190. print("Welcome to the shapifier!")
  191.  
  192. # Which type of shape?
  193. shape_choice = do_menu(
  194. "What shape would you like? ",
  195. [shape.name for shape in known_shapes]
  196. )
  197.  
  198. # Make a shape of that type!
  199. # (because of the way I wrote Shape.__init__,
  200. # it will automatically prompt for required values)
  201. shape = known_shapes[shape_choice]()
  202.  
  203. # Which calculation?
  204. action_choice = do_menu(
  205. "What would you like to calculate? ",
  206. known_actions
  207. )
  208.  
  209. # Perform the calculation
  210. value = getattr(shape, known_actions[action_choice])()
  211.  
  212. # Display the result
  213. print("nThe {} of the {} was {}.".format(known_actions[action_choice], shape.name, value))
  214.  
  215. if __name__=="__main__":
  216. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement