如果我在一个if语句中有多个条件,如何查看哪个条件返回false?

时间:2015-12-06 21:30:28

标签: python if-statement conditional

if (condition_1 and condition_2 and condition_3 or condition_4):
    return True
else:
    return False

3 个答案:

答案 0 :(得分:-1)

x=None
y=None
z="yesitis"
result=["first true" if x and y else "second true" if z else False]

如果x和y存在,它将返回“first true”,或者如果z存在,则返回“second true”。 或者,如果你想要,你也可以返回True

[True,"one"] if x and y else [True,"second"] if y else False

答案 1 :(得分:-1)

因为所有这些实体都被捆绑起来,所以真的无法知道哪个部分返回false,所以有两种方法:

1)将所有条件分成单独的if / else语句。这很容易做到,只需要你重复if / else,一次只包含一个条件。 (EASY)

2)无论你想要实现什么条件,例如问题的特定答案或诅咒的键,在某种意义上尝试“关闭和打开”,并故意试图通过该判断进行虚假命令看哪一个不起作用。这很复杂,可能需要一段时间,并且只能用于您可以控制的选择条件,但如果您希望保留最小的代码行,则可行。 (硬)

答案 2 :(得分:-2)

您可以尝试嵌套这样的条件

if(first == true):
  print("pass on first confition")
  if(second == true):
     print("pass on second condition")
     if(third == true):
        print("Made it till the third condition")
     else:
       print("Could not make it on third condition")
  else:
    print("Could not make it on second condition")
else:
  print("Could not make it on first condition")

就像我的代码一样,但有更好的缩进