具体情况不会在第 8 行通过 elif:

The specific case won't pass elif on line 8:

我在 Dcoder 上解决难题,我决定对解决方案进行硬编码,但它仍然没有通过所有测试。

我将代码扩展到现在的样子(我粘贴的代码)以包括我能想到的所有情况。我发现了一个应该通过的特定案例,但由于某种原因没有发生。

我也尝试在 if/elif 块中添加括号,但这并没有改变任何东西(我没想到会这样,但我还是试过了)

def damn(a, b, c, d, n, m):
    if a+c <= n:
        if b <= m and d <= m:
            return True
    elif a+d <= n:
        if b <= m and c <= m:
            return True
    elif a+c <= m:
        if b <= n and d <= n:
            return True
    elif a+d <= m:
        if b <= n and c <= n:
            return True
    elif b+c <= n:
        if a <= m and d <= m:
            return True
    elif b+d <= n:
        if a <= m and c <= m:
            return True
    elif b+c <= m:
        if a <= n and d <= n:
            return True
    elif b+d <= m:
        if a <= n and c <= n:
            return True
    else:
        return False

if damn(2, 2, 1, 4, 4, 3):
    print("Yes")
else:
    print("No")
elif a+c <= m:
        if b <= n and d <= n:
            return True

应该是:

3 <= 3

2 <= 4 和 4 <= 4

全部输出为真,这些是我在 if/elif/else 块之前打印它们时出现的值,但由于某种原因函数 "damn" returns 为假。

有谁知道为什么会发生这种情况,您能解释一下吗?

您的第一个 if 语句通过了:

if a + c <= n:

所以代码永远不会到达您想要的 elif 分支:

a, b, c, d, n, m  
2, 2, 1, 4, 4, 3

2 + 1 ( 3 ) <= 4

您可能希望合并 if 语句而不是嵌套它们:

if a + c <= n and b <= m and d <= m: