python - 找出哪个条件打破了逻辑 and 表达式

标签 python boolean-expression

如果 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 ,我如何找出 test1test2test3负责返回第一个虚假值?

齐发。

最佳答案

我们可以将该测试值存储在列表中,然后检查它们是否都为 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/

相关文章:

c - 无法找出 C 程序中的逻辑错误

java - 无法创建 while/boolean 循环

javascript - 是否可以使用 javascript 从 django 模型数据库中获取数据?

python - 如何使用 sklearn 管道缩放 Keras 自动编码器模型的目标值?

Python:多次写入文件而无需每次写入都打开/关闭

python - 使用 bool 选择选择一个dataframe,然后提取某一列对应的值

python - 防止 print() 语句覆盖 input() 语句中的文本

python - 截取屏幕的几个区域并使用 PyAutoGui - Python 3.4 保存它们

language-agnostic - 三个值的异或

python - 复杂复合 bool 表达式