为什么我的不等边三角形测试不正常?

0 投票
3 回答
1105 浏览
提问于 2025-04-17 00:08

检查不等边三角形(我正在学习 http://www.pyschools.com

我不知道哪里出错了,因为我无法通过这个测试。

写一个函数 isScalene(x, y, z),这个函数接受三角形的 3 条边 作为输入。如果这个三角形是一个不等边三角形,函数应该返回 True。不等边三角形的特点是没有两条边是相等的。

示例

>>> isScalene(2, 4, 3)
True
>>> isScalene(3, 3, 3)
False
>>> isScalene(0, 2, 3)
False
>>> isScalene(2, 2, 3)
False

我的函数定义是这样的:

def isScalene(x, y, z): 
    if(x > 0 and y >0 and z> 0):
        if(x!=y!=z):
            return True
        else:
            return False
    else:
        return False

有人能给我个建议吗?

3 个回答

0
def isScalene(x, y, z): 
    if x <= 0 or y <= 0 or z <= 0:
            return False
    if x + y > z and x - y < z:
       if x !=y != z:
          return True
    return False

你应该先确认这个不等边三角形确实是一个三角形!

4

如果输入的是2、3、5呢?(提示:根本就不是一个三角形!)

3

试着表达得更清楚一些,我觉得你的 x!=y!=z 可能是个问题。

if ( ( x != y ) and ( x != z ) and ( y !=z ) )

撰写回答