[英]how to write a python program that can tell whether the numbers entered can form a triangle or not?
i was writing the following code:我正在编写以下代码:
def form_triangle(num1,num2,num3):
success="Triangle can be formed"
failure="Triangle can't be formed"
if(num1 < num2 + num3):
if(num2 < num1 + num3):
if(num3 < num1 + num2):
return success
else:
return failure
num1=3
num2=3
num3=5
result = form_triangle(num1, num2, num3)
print(result)
but the problem is this code is unable to pass all the test cases.但问题是这段代码无法通过所有的测试用例。 for instance, if num1, num2, num3 have values 1, 2, 3 respectively, then the expected output should be N/A but my output is None.
例如,如果 num1、num2、num3 的值分别为 1、2、3,那么预期的 output 应该是 N/A,但我的 output 是 None。 so, what changes should i have to made in my code to have the expected output.
那么,我应该在我的代码中进行哪些更改才能获得预期的 output。
use multi condition if statement
as follows使用多条件
if statement
如下
def form_triangle(num1,num2,num3):
success="Triangle can be formed"
failure="Triangle can't be formed"
if(num1 < num2 + num3) and (num2 < num1 + num3) and (num3 < num1 + num2):
return success
else:
return failure
num1, num2, num3 = 1,2,3
result = form_triangle(num1, num2, num3)
print(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.