Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python布尔和逻辑运算符_Python_Boolean_Operator Keyword_Logical Operators - Fatal编程技术网

Python布尔和逻辑运算符

Python布尔和逻辑运算符,python,boolean,operator-keyword,logical-operators,Python,Boolean,Operator Keyword,Logical Operators,给定两个输入布尔值,我想打印出以下结果: 真-真->假 真假->假 假-真->假 假假->真 我试着这样做: if boolInput1 and boolInput2 == True: print(False) elif boolInput1 == True and boolInput2 == False: print(False) elif boolInput1 == False and boolInput2 == True: print(False) elif boo

给定两个输入布尔值,我想打印出以下结果:

真-真->假
真假->假
假-真->假
假假->真

我试着这样做:

if boolInput1 and boolInput2 == True:
    print(False)
elif boolInput1 == True and boolInput2 == False:
    print(False)
elif boolInput1 == False and boolInput2 == True:
    print(False)
elif boolInput1 and boolInput2 == False:
    print(True)
但它不起作用,因为这是输出:

Test  Input    Expected Actual 
1   True True   False   False
2   True False  False   False
3   False True  False   False
4   False False True    False
我试着在网上搜索答案,但什么也找不到

这个怎么样

print(not boolInput1 and not boolInput2)
您的代码存在以下问题:

elif boolInput1 and boolInput2 == False:
    print(True)
如果改为:

elif boolInput1 == False and boolInput2 == False:
    print(True)
尽管存在相同的问题,但这一行仍然可以正常工作,因为
if boolInput1
大致完成了您想要的功能(检查truthy值)

这样写可能更好,以便与其他检查更一致:

if boolInput1 == True and boolInput2 == True:
这个怎么样

print(not boolInput1 and not boolInput2)
您的代码存在以下问题:

elif boolInput1 and boolInput2 == False:
    print(True)
如果改为:

elif boolInput1 == False and boolInput2 == False:
    print(True)
尽管存在相同的问题,但这一行仍然可以正常工作,因为
if boolInput1
大致完成了您想要的功能(检查truthy值)

这样写可能更好,以便与其他检查更一致:

if boolInput1 == True and boolInput2 == True:

boolInput1和boolInput2==False
不会按您的想法执行。
=
绑定得更紧密,因此当您想要“boolInput1 False和boolInput2 False”时,您正在测试“is boolInput1(truthy),boolInput2是否等于False?”,这将以
boolInput1==False和boolInput2==False或更pythonical的方式表示,
非boolInput1和非boolInput2

真的,你让事情变得更难了。您的所有代码都可以简化为:

print(not boolInput1 and not boolInput2)
或者提取
而不是
,如果您愿意:

print(not (boolInput1 or boolInput2))
if
elif
else
或任何其他所需的块


一般来说,显式地与
True
False
进行比较并不符合Pythonic;只需使用隐式“真实性”测试来处理任何类型。因为这里无论如何都需要
not
,所以最终结果将始终是
True
False
,即使输入根本不是布尔值,直接与
True
False
进行比较将产生
2
None
[]
与传统的“真实性测试”(分别为truthy、falsy和falsy)行为方式不同。

boolInput1和boolInput2==False
不会按照您的想法行事。
=
绑定得更紧密,因此当您想要“boolInput1 False和boolInput2 False”时,您正在测试“is boolInput1(truthy),boolInput2是否等于False?”,这将以
boolInput1==False和boolInput2==False或更pythonical的方式表示,
非boolInput1和非boolInput2

真的,你让事情变得更难了。您的所有代码都可以简化为:

print(not boolInput1 and not boolInput2)
或者提取
而不是
,如果您愿意:

print(not (boolInput1 or boolInput2))
if
elif
else
或任何其他所需的块


一般来说,显式地与
True
False
进行比较并不符合Pythonic;只需使用隐式“真实性”测试来处理任何类型。因为这里无论如何都需要
not
,所以最终结果将始终是
True
False
,即使输入根本不是布尔值,直接与
True
False
进行比较将产生
2
None
[]
与传统的“真实性测试”(分别是truthy、falsy和falsy)行为方式不同。

这可能更简单

if bool1 or bool2:
    print(False)
else:
    print(True)
我相信,你也可以这样做

print(not(bool1 or bool2))

这更简单

这可能要简单得多

if bool1 or bool2:
    print(False)
else:
    print(True)
我相信,你也可以这样做

print(not(bool1 or bool2))

这更简单

elif boolInput1和boolInput2==False:
没有做你认为它在做的事情

和的每一侧都作为单独的布尔值进行计算

要压缩计算机在该语句上的操作,请执行以下操作:

boolInput1 and boolInput2 == False
False and False == False
False and True
False #Does not enter if Statement
这应该告诉你,你的逻辑在所有的4实际上是错误的,有办法把它搞砸。尽可能避免使用
boolean==true
类语句,只要说
if boolean

工作版本:

if boolInput1 and boolInput2:
    print(False)
elif boolInput1 and not boolInput2:
    print(False)
elif not boolInput1 and boolInput2:
    print(False)
elif not boolInput1 and not boolInput2:
    print(True)

尽管这取决于您编写此代码的原因,但还有更简单的方法。

elif boolInput1和boolInput2==False:
没有做您认为它正在做的事情

和的每一侧都作为单独的布尔值进行计算

要压缩计算机在该语句上的操作,请执行以下操作:

boolInput1 and boolInput2 == False
False and False == False
False and True
False #Does not enter if Statement
这应该告诉你,你的逻辑在所有的4实际上是错误的,有办法把它搞砸。尽可能避免使用
boolean==true
类语句,只要说
if boolean

工作版本:

if boolInput1 and boolInput2:
    print(False)
elif boolInput1 and not boolInput2:
    print(False)
elif not boolInput1 and boolInput2:
    print(False)
elif not boolInput1 and not boolInput2:
    print(True)

尽管这取决于您编写此代码的原因,但还有更简单的方法。

print(不是boolInput1也不是boolInput2)
@cᴏʟᴅsᴘᴇᴇᴅ 第一种情况将返回
True
,其他情况将返回
False
。@smarx抱歉,我误读了。
print(不是boolInput1也不是boolInput2)
@cᴏʟᴅsᴘᴇᴇᴅ 第一种情况将返回
True
,其他所有情况将返回
False
。@smarx抱歉,我误读了。我想你是反向的。我不确定是否已经进行了编辑,但这仍然是错误的。我相信所有其他答案都是正确的。:-)作为一个测试用例,如果
bool1
True
bool2
False
,该怎么办?正确答案是
False
,但是你的代码会打印
True
@smarx,天哪,我只是在这里失败了,不是吗?让我来修这个,不用修。。。已经有几个正确的答案了。但是如果你想与众不同,可以选择
if bool1或bool2:print(False)
pr