adpkg

A package containing Python mo...
Log | Files | Refs | README | LICENSE

triangle.py (2916B)


      1 # this module had the function to calculate the area of triangle
      2 # √[s(s-a)(s-b)(s-c)]
      3 
      4 def areaoftriangle(len_a, len_b, len_c, unit = '') : 
      5     """
      6     Calculates the area of triangle using general formula.  √[s(s-a)(s-b)(s-c)]  
      7 
      8     Args: 
      9         len_a = A positive number as length of a side of a triangle (Not Zero)
     10         len_b = A positive number as length of a side of a triangle (Not Zero)
     11         len_c = A positive number as length of a side of a triangle (Not Zero)
     12         unit = Unit of the area of the triangle (e.g., "sq cm", "m^2").
     13 
     14     Returns:
     15         Area of the triangle
     16         None if the input is not valid or any error occurs.
     17     """
     18     # first checking if the input values are even valid or not.
     19     try : 
     20         a = float(len_a)
     21         b = float(len_b)
     22         c = float(len_c)
     23     except ValueError :
     24         print('Please enter a valid number (Integer or Float).')
     25         return None
     26     except Exception as error : 
     27         print(f'An unexpected error happened : {error}')
     28         return None
     29     
     30     # checking if inputs are above 0, because a side length can't be zero
     31     if a <= 0 or b <= 0 or c <= 0 :
     32         print('Side length of a triangle can\'t be Zero or negative.')
     33         return None
     34     
     35     # checking if the triangle even follows the triangle rule : len_a + len_b > len_c
     36     if not (a + b >= c and a + c >= b and b + c >= a) :
     37         print('The triangle must satisfy the triangle inequality theorem (the sum of the lengths of any two sides must be greater than or same as the length of the third side).')
     38         return None
     39     
     40     # main logic
     41     try :
     42         # calculating the value of 's' (The semi-perimeter (half the perimeter) of the triangle) 
     43         s = (a + b + c) / 2
     44 
     45         # now the formula without root
     46         pre_result = (s * (s - a) * (s - b) * (s - c))
     47 
     48         if pre_result < 0:
     49         # Due to floating point inaccuracies, it might be slightly negative
     50             if pre_result > -0.09 : # A small tolerance
     51                 result = 0.0
     52             else:
     53                 print("Error: Calculation resulted in a negative number under the square root, indicating invalid triangle sides (likely due to precision issues)."
     54                       f"\nCalculated value before root was : {pre_result}")
     55                 return None
     56 
     57         else : 
     58             # now the root
     59             result = pre_result ** 0.5
     60 
     61     except Exception as error : 
     62         print(f'An unexpected error happened : {error}')
     63         return None
     64 
     65     # returning the result with the unit if provided   
     66     try : 
     67         if len(unit) > 0 :
     68             return f'{result:.3f} {unit}'
     69         else :
     70             result = round(result, 3)
     71             return float(result)
     72     except Exception as error : 
     73         print(f'An unexpected error happened : {error}')
     74         return None
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror