Python Program to Check Types of Triangle Given Sides


Table of Contents

There are three types of triangle based on the length of the sides: equilateral, isosceles, and scalene.

This python program checks whether a triangle is equilateral or isosceles or scalene based on given length of sides.

Python Source Code: Triangle Type


# Validity of Triangle given sides

# Function definition to check validity
def is_valid_triangle(a,b,c):
    if a+b>=c and b+c>=a and c+a>=b:
        return True
    else:
        return False

# Function definition for type
def type_of_triangle(a,b,c):
    if a==b and b==c:
        print('Triangle is Equilateral.')
    elif a==b or b==c or a==c:
        print('Triangle is Isosceles.')
    else:
        print('Triangle is Scalane')

# Reading Three Sides
side_a = float(input('Enter length of side a: '))
side_b = float(input('Enter length of side b: '))
side_c = float(input('Enter length of side c: '))

# Function call & making decision
if is_valid_triangle(side_a, side_b, side_c):
    type_of_triangle(side_a, side_b, side_c)
else:
    print('Tringle is not possible from given sides.')

Python Output: Triangle Type

Run 1:
------------
Enter length of side a: 4
Enter length of side b: 8
Enter length of side c: 6
Triangle is Scalane

Run 2:
------------
Enter length of side a: 6
Enter length of side b: 6
Enter length of side c: 12
Triangle is Isosceles.

Run 3:
------------
Enter length of side a: 5
Enter length of side b: 5
Enter length of side c: 5
Triangle is Equilateral.

Run 4:
Enter length of side a: 3
Enter length of side b: 6
Enter length of side c: 20
Tringle is not possible from given sides.