如果 if
block 不是,我想找到一种优雅的方法来获取下面负责的逻辑 and
表达式的组件被处决。
if test1(value) and test2(value) and test3(value):
print 'Yeeaah'
else:
print 'Oh, no!', 'Who is the first function that return false?'
如果输入了 else
block ,我如何找出 test1
、test2
或 test3
负责返回第一个虚假值?
齐发。
最佳答案
我们可以将该测试值存储在列表中,然后检查它们是否都为 True,如果不为 False,则打印第一个值的索引。
def test1(x):
return True
def test2(x):
return False
def test3(x):
return True
value = 'a'
statements = [test1(value), test2(value), test3(value)]
if all(statements):
print('Yeeaah')
else:
print('Oh, no! Function that first return False is {}'.format(statements.index(False)))
输出:
Oh, no! Function that first return False is 1
关于python - 找出哪个条件打破了逻辑 and 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53430489/