36. Triangle type
Three sticks make a triangle only if each one is shorter than the other two laid end to end. With lengths 1, 2 and 3 the two short sticks only just reach across the long one, so they lie flat: that isn't a triangle.
Write a function triangle_type(a, b, c) that takes three whole numbers, the lengths of the sides, and returns one of
four strings:
"not a triangle"if the sides can't make a triangle: some side is as long as the other two together, or longer. A side of 0 can never be part of a triangle, and this rule already rules it out."equilateral"if all three sides are the same length;"isosceles"if exactly two sides are the same length;"scalene"if all three lengths are different.
Check for "not a triangle" first: 2, 2 and 5 have two equal sides, but 2 + 2 is less than 5.
Example 1
a = 3, b = 3, c = 3"equilateral"Example 2
a = 3, b = 4, c = 5"scalene"Example 3
a = 1, b = 2, c = 3"not a triangle"Constraints
a,bandcare whole numbers from 0 to 1000, in any order.
Submit also runs 11 more tests you can't see.
Practises If / Elif / Else (lesson 8).
- Test for a real triangle first: every side must be shorter than the other two together, so check
a + b > c,a + c > bandb + c > a. If any of them fails, return"not a triangle". a == b == cchecks that all three sides are equal in one go.- After that, any two equal sides make it isosceles:
a == b or b == c or a == c. Whatever is left is scalene. Try 2, 2, 5: it must be"not a triangle", not isosceles.
Solve it without hints for 3 stars, with hints for 2, after seeing the solution for 1.
One way to solve it, once you've had a go.
Solving it after seeing the solution earns 1 star.
Your Submits show here, with their code, so you can go back to one. They stay in this browser.
def triangle_type(a, b, c):
"""Name the kind of triangle three sides make.
a, b, c: int, the lengths of the sides, 0 to 1000
Returns: str, "equilateral", "isosceles", "scalene" or
"not a triangle"
"""
# Write your code here
pass
Run your code to see how it does on the test cases, or Submit to check every test.