Skip to content

script_9.py - Triangle Type Identifier

Code

Python
#!/usr/bin/env python3
"""Triangle type identifier"""

a = float(input("Enter side a: "))
b = float(input("Enter side b: "))
c = float(input("Enter side c: "))

if a == b == c:
    print("Equilateral triangle")
elif a == b or b == c or a == c:
    print("Isosceles triangle")
else:
    print("Scalene triangle")

Explanation

Line 4-6: Variable assignments - Three separate input() calls - Each converted to float type to handle decimal values - a, b, c variables store the three side lengths

Line 8: if a == b == c: - Chained comparison - Python allows comparing multiple values - Equivalent to (a == b) and (b == c) - All three values must be equal - Returns True only if all three sides are identical

Line 10: elif a == b or b == c or a == c: - or operator - returns True if ANY condition is True - Checks three pairs: a with b, b with c, a with c - Returns True if at least two sides are equal - Only evaluated if line 8 was False (not all three equal)

Line 12: else: - Executes when no sides are equal - All three sides must be different lengths - Scalene triangle has no equal sides