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.
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 # Valid triangle - determine type9 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
Check the outer triangle-validity condition.
- 2
Test three equal sides before any equal pair.
- 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.
๐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