-2

I've been trying to write a program that determines if three lines can make a triangle or not. However, the code I have written does not work and I can not seem to determine why. It is currently producing 'no' for any inputs.(Note: The largest side has to be smaller then the sum of the other two sides in order to make a triangle)

My code:

def is_triangle(a,b,c):
    if a >= b and a >= c and (b + c) >= a:
        return print('yes')
    elif b >= c and b >= a and (a + c) >= b:
        return print('yes')
    elif c >= a and c >= b and (a + b) >= c:
        return print('yes')
    else:
        return print('no')


def input_triangle():
    a = input('first side?')
    b = input('second side?')
    c = input('third side?')
    return is_triangle(a,b,c)

input_triangle()
  • Probably need to turn your `input` into numbers, with `int(input("first side?"))` or `float(input("first side?"))`. Otherwise, it's comparing the strings – dwanderson Mar 18 '16 at 21:22

1 Answers1

0

int or float the input values; Currently you're doing string comparison and string concatenation rather than math on your inputs.

a = int(input("first side?"))
## OR
a = float(input("first side?"))

When doing it with strings, say for argument's sake the user inputs 5, 4, 3, which of course should be valid. Your test is doing: "5" > "4" and "4" > "3" (good so far...) and finally and "43" > "5" which is a) not what you want and b) false (it goes character by character, and "4" is not > "5").

dwanderson
  • 2,775
  • 2
  • 25
  • 40