w3resource

Python Exercise: Check a triangle is equilateral, isosceles or scalene


Write a Python program to check if a triangle is equilateral, isosceles or scalene.
Note :
An equilateral triangle is a triangle in which all three sides are equal.
A scalene triangle is a triangle that has three unequal sides.
An isosceles triangle is a triangle with (at least) two equal sides.

Pictorial Presentation:

Python Exercise: Check a triangle is equilateral, isosceles or scalene

Sample Solution:

Python Code:

# Display a message prompting the user to input lengths of the sides of a triangle
print("Input lengths of the triangle sides: ")

# Request input from the user for the length of side 'x' and convert it to an integer
x = int(input("x: "))

# Request input from the user for the length of side 'y' and convert it to an integer
y = int(input("y: "))

# Request input from the user for the length of side 'z' and convert it to an integer
z = int(input("z: "))

# Check conditions to determine the type of triangle based on the lengths of its sides

# If all sides are equal, display that it's an equilateral triangle
if x == y == z:
    print("Equilateral triangle")
# If at least two sides are equal, display that it's an isosceles triangle
elif x == y or y == z or z == x:
    print("Isosceles triangle")
# If all sides have different lengths, display that it's a scalene triangle
else:
    print("Scalene triangle") 

Sample Output:

x: 6                                                                                                          
y: 8                                                                                                          
z: 12                                                                                                         
Scalene triangle  

Flowchart :

Flowchart: Check a triangle is equilateral, isosceles or scalene

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to check a string represent an integer or not?
Next: Write a Python program that reads two integers representing a month and day and prints the season for that month and day.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.