Calculate Third Angle

Take two angles of a triangle and compute the third angle.

IntermediatePhase 1 - Level 3: Math and Number LogicExample 9 of 10
calculate-third-angle.py
1# Take two angles
2angle1 = float(input("Enter first angle: "))
3angle2 = float(input("Enter second angle: "))
4
5# Calculate third angle
6third_angle = 180 - angle1 - angle2
7
8# Validate
9if third_angle > 0 and angle1 > 0 and angle2 > 0:
10 print(f"Third angle: {third_angle} degrees")
11else:
12 print("Invalid triangle angles")

Output

Enter first angle: 60
Enter second angle: 60
Third angle: 60.0 degrees

Enter first angle: 90
Enter second angle: 45
Third angle: 45.0 degrees

What's going on

Sum of angles in triangle is 180 degrees.

Key Concepts:

Third angle = 180 - angle1 - angle2
Validate that all angles are positive
Check if valid triangle