Skip to content

Instantly share code, notes, and snippets.

@amirsinaa
Last active March 16, 2024 05:59
Show Gist options
  • Save amirsinaa/0172eadf67c25186c899a58a8ad318e3 to your computer and use it in GitHub Desktop.
Save amirsinaa/0172eadf67c25186c899a58a8ad318e3 to your computer and use it in GitHub Desktop.
Python code that calculate the area of a triangle and recognize its type (equilateral - isosceles - different-sided - right-angled)
import math
def calculate_area(a, b, c):
s = (a + b + c) / 2
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
return area
def classify_triangle(a, b, c):
if a == b == c:
return "equilateral"
elif a == b or b == c or a == c:
return "isosceles"
else:
return "different-sided"
def check_right_triangle(a, b, c):
sides = [a, b, c]
sides.sort()
if sides[0]**2 + sides[1]**2 == sides[2]**2:
return "is a right-angled"
else:
return "is not a right-angled"
def main():
sides = input("Enter the sides of the triangle separated by space: ").split()
a, b, c = map(float, sides)
area = calculate_area(a, b, c)
triangle_type = classify_triangle(a, b, c)
right_triangle = check_right_triangle(a, b, c)
print(f"The area of the triangle is: {area}")
print(f"The triangle is an {triangle_type} triangle")
print(f"The triangle {right_triangle} triangle.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment