Founding Batch Offer · HLD · LLD · System Design at ₹799 until 30 Nov

Triangle Type Check in Python

Validate three Python side lengths before naming the triangle. First-year logic practice covers strict inequalities, equal sides, and the flat 1-2-3 case.

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

Scalene triangle

3, 4, 5 passes the validity tests and has no equal pair, so it is scalene.

In this one

  • Validate triangle inequalities
  • Order specific tests before general ones
  • Use chained equality comparisons

How it works

  1. 1

    Reject sides <= 0.

  2. 2

    Reject any pair sum <= the remaining side.

  3. 3

    Check three equal sides, then any equal pair, otherwise print scalene.

Edge cases

Flat shape

Enter 1, 2, 3.

The equality 1 + 2 == 3 fails the strict triangle requirement.

All equal

Enter 4, 4, 4.

The equilateral check wins before the isosceles condition.

What's going on

First ask whether the sides form a triangle at all. Each side must be positive, and any two sides together must be longer than the third. Equality gives a flat line, not a triangle.

Only after those checks does the code classify the shape. a == b == c means all three lengths match, so it is equilateral. Otherwise, any equal pair gives isosceles. If no pair matches, it is scalene.

Order matters here too. Testing for any equal pair first would catch an equilateral triangle before the more specific label. The sample 3, 4, 5 is valid and has no equal sides, so it is scalene. This lesson uses direct equality for entered lengths; calculated measurements may need a chosen tolerance.

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.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why reject a + b == c?

Pick an answer to check it.

Question 2

Why test equilateral first?

Pick an answer to check it.

Questions people ask

Why reject a + b == c?

The sides form a flat line, so their sum must be strictly greater than the third side.

Why test equilateral first?

Every equilateral triangle also has an equal pair, so the specific label must come first.

Does this identify right angles?

No. It classifies by equal side lengths, not by angle measurements.