Advertisement
pamalau

102.wk1.7

Jan 30th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. from math import sqrt
  2. """
  3. This function is based on the Triangle Inequality Theorem, which states
  4. that the sum of two side lengths of a triangle is always greater
  5. than the third side. If this is true for all three combinations of added
  6. side lengths, then the sides will form a triangle.
  7. """
  8. def Is_a_Triangle(a, b, c):
  9.     result = ( (a+b) > c) and ( (a+c) > b) and ( (b+c) > a)
  10.     return(result)
  11.  
  12. """
  13. This function is based on Heron's Formula
  14. https://en.wikipedia.org/wiki/Heron%27s_formula
  15. It uses the numerically stable version. Point 5 in the above link.
  16. This, expressed in terms of the variables used is
  17. Area = Sqrt(w*x*y*z)/4
  18. """
  19. def Area_of_a_Triangle(a, b, c):
  20.     w = (a + (b+c))
  21.     x = (c - (a-b))
  22.     y = (c + (a-b))
  23.     z = (a + (b-c))
  24.     return(sqrt(w * x * y * z) / 4.0)
  25.    
  26. print("Calculating the area of a triangle.")
  27. print("Input the length of each side, a,b,c, in consistent units.")  
  28. print("This means all sides in cm for example.")
  29. Finished = False
  30. while not Finished:
  31.     a = input("Length of side a (0 to exit) : ")
  32.     Finished  = (int(a) == 0)
  33.     if not Finished:
  34.         a = float(a)
  35.         b = input("Length of side b : ")
  36.         b = float(b)
  37.         c = input("Length of side c : ")
  38.         c = float(c)
  39.         if Is_a_Triangle(a, b, c):
  40.             Area = Area_of_a_Triangle(a, b, c)
  41.             print("Area of the triangle is : %.2f"% (Area))
  42.         else:
  43.             print(str(a) + ", " + str(b)  + ", " + str(c) + " is not triangle")
  44.            
  45. print("bye")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement