Program that accepts the lengths of three sides of a triangle as inputs.
Write the following two programs using Python Language.
Q1. Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is an equilateral triangle.
Python
def is_equilateral(side1, side2, side3):
"""Determines if a triangle is equilateral based on its side lengths."""
return side1 == side2 == side3
# Get the lengths of the triangle's sides from the user
side1 = float(input("Enter the length of side 1: "))
side2 = float(input("Enter the length of side 2: "))
side3 = float(input("Enter the length of side 3: "))
# Check if the triangle is equilateral
if is_equilateral(side1, side2, side3):
print("The triangle is equilateral.")
else:
print("The triangle is not equilateral.")
Explanation:
-
is_equilateralfunction:- Takes three side lengths as input.
- Returns
Trueif all three sides are equal, indicating an equilateral triangle. - Returns
Falseotherwise.
-
Input:
- Prompts the user to enter the lengths of the three sides using
input(). - Converts the inputs to floating-point numbers using
float().
- Prompts the user to enter the lengths of the three sides using
-
Check for equilateral triangle:
- Uses the
ifstatement to check the result of theis_equilateralfunction. - Prints the appropriate message based on the result.
- Uses the