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