user1180169 - 3 months ago 5

Python Question

I came across an exercise in "Think Python", and I couldn't figure out how to do it.

The exercise: 1. **Write a function named is_triangle that takes three integers as arguments, and that prints either “Yes” or “No,” depending on whether you can or cannot form a triangle from sticks with the given lengths.**

And of course: **“If any of the three lengths is greater than the sum of the other two, then you cannot form a triangle. Otherwise, you can3.”**

Answer

Plenty of ways to do this, here is one:

```
def is_triangle(a, b, c):
if (a > b + c) or (b > a + c) or (c > a + b):
print "No"
else:
print "Yes"
```

Source (Stackoverflow)

Comments