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

Check if Three Sides Form a Valid Triangle

Check whether three lengths can form a triangle using Python. Students trace all three strict inequalities and distinguish a flat shape from a valid triangle.

IntermediatePhase 1 - Level 2: Nested If & Multiple ConditionsExample 1 of 10
check-if-three-sides-form-a-valid-triangle.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 print("Valid triangle")
9else:
10 print("Invalid triangle")

Output

Enter side 1: 3
Enter side 2: 4
Enter side 3: 5
Valid triangle

Enter side 1: 1
Enter side 2: 2
Enter side 3: 5
Invalid triangle

In this one

  • Apply triangle inequalities
  • Require every constraint
  • Reject flat triangles

How it works

  1. 1

    Read three side lengths as floats.

  2. 2

    Compare every pair sum with the remaining side.

  3. 3

    Print valid only when all strict inequalities hold.

Edge cases

Do sides 1, 2, 3 pass?

Do sides 1, 2, 3 pass?

No. The first sum equals the third side rather than exceeding it.

Can a nonpositive side pass all tests?

Can a nonpositive side pass all tests?

For ordinary finite inputs, no; the three strict inequalities exclude it.

What's going on

Three sides form a triangle only when every pair adds to more than the remaining side. The code reads three floating-point lengths and joins all three tests with and.

For 3, 4, and 5, each pair passes. For 1, 2, and 5, the first pair is too short. Equality is not enough: sides 1, 2, and 3 lie flat rather than enclosing an area.

Do sides 1, 2, 3 pass? No. The first sum equals the third side rather than exceeding it.

Can a nonpositive side pass all tests? For ordinary finite inputs, no; the three strict inequalities exclude it.

Quiz

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

Question 1

Why test all three pairs?

Pick an answer to check it.

Question 2

Do sides 1, 2, 3 pass?

Pick an answer to check it.

Questions people ask

Do sides 1, 2, 3 pass?

No. The first sum equals the third side rather than exceeding it.

Can a nonpositive side pass all tests?

For ordinary finite inputs, no; the three strict inequalities exclude it.

Why test all three pairs?

Any side might be the longest.