Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Conditional Statements

Determine if a triangle is scalene, isosceles, or equilateral - Python Program

Example 1 :

a = float(input("Enter the length of the first side: ")) b = float(input("Enter the length of the second side: ")) c = float(input("Enter the length of the third side: ")) if a == b == c: print("Equilateral") elif a == b or b == c or a == c: print("Isosceles") else: print("Scalene")

Output

 
OUTPUT 1 :
Enter the length of the first side: 5
Enter the length of the second side: 5
Enter the length of the third side: 5
Equilateral 

OUTPUT 2 :

Enter the length of the first side: 10
Enter the length of the second side: 10
Enter the length of the third side: 12
Isosceles

OUTPUT 3 :
Enter the length of the first side: 3
Enter the length of the second side: 4
Enter the length of the third side: 5
Scalene
 

Example 2 :

def classify_triangle(side1, side2, side3): """ Classifies a triangle as equilateral, isosceles, or scalene based on its side lengths. Args: side1 (float): Length of the first side. side2 (float): Length of the second side. side3 (float): Length of the third side. Returns: str: The type of the triangle ("Equilateral", "Isosceles", or "Scalene"). """ if side1 == side2 and side2 == side3: return "Equilateral" elif side1 == side2 or side2 == side3 or side1 == side3: return "Isosceles" else: return "Scalene" # Input from the user try: s1 = float(input("Enter the length of the first side: ")) s2 = float(input("Enter the length of the second side: ")) s3 = float(input("Enter the length of the third side: ")) # Check for valid triangle (triangle inequality theorem) if (s1 + s2 > s3) and (s1 + s3 > s2) and (s2 + s3 > s1): triangle_type = classify_triangle(s1, s2, s3) print(f"The triangle is: {triangle_type}") else: print("The entered side lengths do not form a valid triangle.") except ValueError: print("Invalid input. Please enter numeric values for side lengths.")

Output

 
OUTPUT 1 :
Enter the length of the first side: 5
Enter the length of the second side: 5
Enter the length of the third side: 5
The triangle is: Equilateral

OUTPUT 2 :

Enter the length of the first side: 10
Enter the length of the second side: 10
Enter the length of the third side: 12
The triangle is: Isosceles

OUTPUT 3 :
Enter the length of the first side: 3
Enter the length of the second side: 4
Enter the length of the third side: 5
The triangle is: Scalene

OUTPUT 4 :
Enter the length of the first side: 3
Enter the length of the second side: 4
Enter the length of the third side: 15
The entered side lengths do not form a valid triangle.