Triangle Type Check in Python

Determine if three sides form a valid triangle and its type (equilateral, isosceles, scalene).

BeginnerConditional ProgramsExample 8 of 20
triangle-type-check.py
1# Program to check triangle validity and type
2
3a = float(input("Enter first side: "))
4b = float(input("Enter second side: "))
5c = float(input("Enter third side: "))
6
7if a <= 0 or b <= 0 or c <= 0:
8 print("Sides must be positive.")
9elif a + b <= c or a + c <= b or b + c <= a:
10 print("Not a valid triangle.")
11else:
12 if a == b == c:
13 print("Equilateral triangle")
14 elif a == b or a == c or b == c:
15 print("Isosceles triangle")
16 else:
17 print("Scalene triangle")

Output

Enter first side: 3
Enter second side: 4
Enter third side: 5
Scalene triangle

What's going on

First we apply the triangle inequality to check validity.

Then we compare side lengths to classify as equilateral, isosceles, or scalene.

After this program

If the idea is still fuzzy, read Day 8: Conditional Statements in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 20 conditional programs, or see every topic on Python exercises.