Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Determine Triangle Type

Classify a valid triangle as equilateral, isosceles, or scalene. A Python exercise for beginners on nested conditions, validation, and ordering equality tests.

IntermediatePhase 1 - Level 2: Nested If & Multiple ConditionsExample 2 of 10
determine-triangle-type.py
1# Take three sides as input
2a = float(input("Enter side 1: "))
3b = float(input("Enter side 2: "))
4c = float(input("Enter side 3: "))
5
6# Check if valid triangle
7if a + b > c and b + c > a and c + a > b:
8 # Valid triangle - determine type
9 if a == b == c:
10 print("Equilateral")
11 elif a == b or b == c or c == a:
12 print("Isosceles")
13 else:
14 print("Scalene")
15else:
16 print("Invalid triangle")

Output

Enter side 1: 5
Enter side 2: 5
Enter side 3: 5
Equilateral

Enter side 1: 5
Enter side 2: 5
Enter side 3: 6
Isosceles

In this one

  • Validate before classifying
  • Recognize equal-side patterns
  • Order overlapping conditions

How it works

  1. 1

    Check the outer triangle-validity condition.

  2. 2

    Test three equal sides before any equal pair.

  3. 3

    Print the side type or the outer invalid message.

Edge cases

Why check equilateral first?

Why check equilateral first?

Otherwise three equal sides would match the isosceles test.

What happens to 1, 2, 9?

What happens to 1, 2, 9?

Only Invalid triangle is printed; the inner tests never run.

What's going on

The outer condition checks that a triangle exists before assigning it a type. This prevents lengths such as 1, 2, and 9 from receiving a misleading side-based label.

Inside the valid branch, a == b == c recognizes three equal sides. The next test needs just one equal pair. It comes second because an equilateral triangle also has equal pairs. The final inner else means all three sides differ.

Why check equilateral first? Otherwise three equal sides would match the isosceles test.

What happens to 1, 2, 9? Only Invalid triangle is printed; the inner tests never run.

Quiz

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

Question 1

Which input is scalene?

Pick an answer to check it.

Question 2

Why check equilateral first?

Pick an answer to check it.

Questions people ask

Why check equilateral first?

Otherwise three equal sides would match the isosceles test.

What happens to 1, 2, 9?

Only Invalid triangle is printed; the inner tests never run.

Which input is scalene?

3, 4, 5.