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.
1# Take three sides as input2a = float(input("Enter side 1: "))3b = float(input("Enter side 2: "))4c = float(input("Enter side 3: "))56# Check if valid triangle7if 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
Read three side lengths as floats.
- 2
Compare every pair sum with the remaining side.
- 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.
๐Related Content
- ๐
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- ๐
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- ๐ป
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum