【问题标题】:Why does my program not return my required value?为什么我的程序没有返回我需要的值?
【发布时间】:2022-09-27 20:41:09
【问题描述】:
def f(v1, v2, v3):
    if v1 == v2 and v2 == v3:
        print(\"Applying f to all three values gives the same result\" + str(v1))

    if v1 == v2 and v1 != v3:
        print(\"Only applying f to x and y gives the same result\")
        return v1
    if v2 == v3 and v2 != v1:
        print(\"Only applying f to y and z gives the same result\")
        return v2
    if v1 == v3 and v1 != v2:
        print(\"Only applying f to x and z gives the same result\")
        return v1
    if v1 != v2 and v2 != v3:
        print(\"Applying f to x,y,z gives all different results\")


def check_equal(f, x, y, z):
    f(x, y, z)


tests = [(42, 1, 42), (1, 1, -5), (5, 4, -1), (5, 5, 5), (0, 0, 1), (-9, 9, 9), (9, 8, 90)]
for x, y, z in tests:
    print(check_equal(f, x, y, z))

此代码检查哪些值:x、y 和 z 相似并向用户打印它们是否相同或完全不同,如果整数匹配则返回 a 值。

检查工作,但我似乎无法返回一个值,只是打印 \"none\" 到控制台

  • 请更新代码的缩进。 Python 对缩进非常敏感,python 程序员也是如此。
  • 好吧,您只在 2,3 和 4 if 语句中返回一个值,所以如果不是该函数返回 None (默认)

标签: python


【解决方案1】:

check_equal 函数不返回任何内容...将其更改为

def check_equal(f, x, y, z):
    return f(x, y, z)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 2011-08-09
    • 2019-06-14
    • 1970-01-01
    • 2015-05-09
    • 2018-05-29
    相关资源
    最近更新 更多