【问题标题】:Python Boolean and Logical OperatorsPython 布尔和逻辑运算符
【发布时间】:2017-07-07 00:35:01
【问题描述】:

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

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

我试过这样做:

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)
  • @cᴏʟᴅsᴘᴇᴇᴅ 第一种情况将返回True,其他情况返回False
  • @smarx 对不起,我看错了。

标签: python boolean operator-keyword logical-operators


【解决方案1】:

boolInput1 and boolInput2 == False 不会按照你的想法去做。 ==and 绑定得更紧密,所以当你想要“boolInput1 False 和 boolInput2 False 吗?”时,您正在测试“boolInput1 是否(真实),并且 boolInput2 是否等于 False?”,这将是以boolInput1 == False and boolInput2 == False 或更多 Python 表示,not boolInput1 and not boolInput2

真的,你让这件事变得比需要的更难。您的所有代码都可以简化为:

print(not boolInput1 and not boolInput2)

如果您愿意,也可以提取 not

print(not (boolInput1 or boolInput2))

不需要ifelifelse 或任何其他块。

一般来说,明确比较TrueFalse 不是Pythonic;只需使用隐式“真实性”测试来处理任何类型。由于无论如何您都需要not,因此最终结果将始终为TrueFalse,即使输入根本不是布尔值,直接与TrueFalse 进行比较将使输入像@ 987654339@、None[] 的行为方式与它们在“真实性测试”中的传统行为方式不同(它们分别为真、假和假)。

【讨论】:

    【解决方案2】:

    这可能要简单得多。

    if bool1 or bool2:
        print(False)
    else:
        print(True)
    

    我相信你也可以这样做

    print(not(bool1 or bool2))
    

    这更简单。

    【讨论】:

    • 我认为你倒退了。
    • 我不确定是否已经进行了编辑,但这仍然是错误的。我相信所有其他答案都是正确的。 :-)
    • 作为一个测试用例,如果bool1Truebool2False呢?正确答案是False,但您的代码将打印True
    • @smarx,天哪,我只是在这里失败了,不是吗?让我解决这个问题。
    • 无需修正……已经有几个正确答案了。但是,如果您想与众不同,请选择 if bool1 or bool2: print(False)print(not(bool1 or bool2))
    【解决方案3】:

    这个怎么样?

    print(not boolInput1 and not boolInput2)
    

    您的代码有问题:

    elif boolInput1 and boolInput2 == False:
        print(True)
    

    如果是这样的话,那就行了:

    elif boolInput1 == False and boolInput2 == False:
        print(True)
    

    尽管存在相同类型的问题,但该行运行良好,因为 if boolInput1 大致完成了您想要的操作(检查真实值)。

    if boolInput1 and boolInput2 == True:
    

    最好以这种方式编写它以与您的其他检查更加一致:

    if boolInput1 == True and boolInput2 == True:
    

    【讨论】:

      【解决方案4】:

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

      and 的每一边都被评估为单独的布尔值。

      在该语句上浓缩计算机正在做什么:

      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)
      

      虽然取决于您编写此代码的原因,但还有更简单的方法可以做到这一点。

      【讨论】:

        猜你喜欢
        • 2014-05-10
        • 1970-01-01
        • 2014-02-20
        • 1970-01-01
        • 2017-01-11
        • 2018-06-05
        • 2015-02-06
        相关资源
        最近更新 更多