【发布时间】: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