def triangle(side1, side2, side3):
if side1 >= side2 + side3 or side2 >= side3 +side1 or side3 >= side1 + side2:
return 'Not a triangle'
elif side1 == side2 == side3:
return 'Equilateral'
elif side1 != side2 != side3:
return 'Scalene'
return 'Isosceles'
代码工作正常,但我想知道是否有更有效的方法来编写代码的第 2 行 '如果三角形的任何一边大于或等于其他两条边的总和':
这个怎么样-
def triangle(side1, side2, side3):
sides = [side1, side2, side3]
if max(sides) >= sum(sides) - max(sides):
return 'Not a triangle'
elif side1 == side2 == side3:
return 'Equilateral'
elif side1 != side2 and side2 != side3 and side1 != side3:
return 'Scalene'
return 'Isosceles'