user1180169 user1180169 - 3 months ago 5
Python Question

Python exercise

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.”


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"
        print "Yes"