Check if Three Sides Form a Valid Triangle

Take three sides and check if they form 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

What's going on

Triangle inequality theorem: Sum of any two sides must be greater than the third side.

Key Concepts:

Check all three combinations: a+b>c, b+c>a, c+a>b
Use and to ensure all conditions are met
All three conditions must be True for a valid triangle