简体   繁体   English

如何编写一个 python 程序来判断输入的数字是否可以形成三角形?

[英]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.

相关问题 程序检查三角形的面积(是否可以根据输入的边进行计算)。 Python-3.x - Program to check the area of a triangle if it can or can not be computed based on the sides entered. Python-3.x 如何在python中以函数方式生成三角形数列表? - How can you generate a list of triangle numbers in python, in a functional manner? 如何在 Python 中复制三角形数字 - How I can copy triangle shape numbers in Python 如何让我的代码编写一个 python 三角形? - How can I make my code write a python triangle? 如果在python中输入数字,如何发出错误消息 - How can I make an error message if numbers are entered in python 如何判断屏幕是否正在运行? - How can I tell whether screen is running? [Python]如何编写一个要求多个数字并将它们相加直到有0的程序? - [Python ]How can I write a program which is asking for multiple numbers and adding them together until there is a 0? 如何编写 python 程序来打印用逗号分隔的一串数字的总和? - How can I write a python program to print the sum of a string of numbers that are separated by a comma? 你能用字符频率来判断给定的文件是 Python 程序文件、C 程序文件还是文本文件? - Can you use character frequency to tell whether the given file is a Python program file, C program file or a text file? 我如何告诉 Python 在元组值之间写一个逗号? - How can I tell Python to write a comma between tuple values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM