【问题标题】:Evaluating Boolean logic with 3 or more variables使用 3 个或更多变量评估布尔逻辑
【发布时间】:2020-06-09 21:43:26
【问题描述】:

我在评估三个布尔变量时试图了解 Python 的行为。

>>> True and True and False == False
True

评估为 'True',这是我所期望的。

然而

>>> True and False and True == False
False
>>> False and True and True == False
False

当我预期 True 时,两者都评估为 'False'。有人可以帮助我了解我在这里缺少什么。我尝试过搜索,但找不到在单个语句中评估 3 个布尔变量的示例。

谢谢!

【问题讨论】:

    标签: python-3.x boolean boolean-expression


    【解决方案1】:

    这是我能想到的解释

    False and True and True == False
    False and True and False        ( because True == False results in False)
    (False and True) and False
    False and False                 ( because 0 and 1 is 0)
    False                           ( because 0 and 0 is 0)
    
    
    True and False and True == False
    True and False and False        ( because True == False results in False)
    (True and False) and False      
    False and False                 ( because 0 and 1 is 0)
    False                           ( because 0 and 0 is 0)
    

    【讨论】:

      【解决方案2】:

      它假设像三个输入和门表

      A   B   C   output
      0   0   0   0
      0   0   1   0
      0   1   0   0
      0   1   1   0
      1   0   0   0
      1   0   1   0
      1   1   0   0
      1   1   1   1
      
      
      True and True and False == False  # 0 and 0 and 0 the answer is 0 (False)
      False and True and True == False  # 0 and 1 and 0 the answer is 0 (False)
      False and True and True == False  # 0 and 1 and 0 the answer is 0 (False)
      

      如果您想了解更多变量

      True and True and True and True and False  
       # 1 and 1 and 1 and 1 and 0  output will be 0 (False)
       # 1 * 1 * 1 * 1 * 0 output will be 0 (False)
      
      True and True and True and True and True 
       # 1 and 1 and 1 and 1 and 1  output will be 1 (True)
       # 1 * 1 * 1 * 1 * 1 output will be 1 (True)
      
      True and False or True or True or True 
      # 1 and 1 or 1 or 1 or 1  output will be 1 (True)
      # 1 * 1 + 1 + 1 + 1 output will be 1 (True)
      

      【讨论】:

      • Ananth,你所描述的是我所期望的行为。但是,如果您在 python 控制台中输入“False and True and True == False”,它将评估为“False”。
      • 我已经用行为截图更新了我的原始帖子
      • @ilmatic9000 当然是假的,你能告诉我你不明白的地方吗?
      【解决方案3】:

      好的,我已经解决了。

      '==' 运算符具有优先权,并首先被评估。

      如果我将左边的语句括在括号中,那么我会得到https://stackoverflow.com/users/10384101/ananth-p 发布的响应中指定的预期行为

      带括号

      Python 3.7.4 (default, Aug 13 2019, 15:17:50) 
      [Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
      Type "help", "copyright", "credits" or "license" for more information.
      
      >>> (True and True and False) == False
      True
      >>> (True and False and True) == False
      True
      >>> (False and True and True) == False
      True
      

      不带括号

      >>> True and True and False == False
      True
      >>> True and False and True == False
      False
      >>> False and True and True == False
      False
      >>> 
      

      【讨论】:

      • 对了,我帮你了吗?
      • 问题是我没有将左边的语句括在括号中。 Python 在评估导致我在最初帖子中观察到的行为的“和”之前评估了“==”。所以是的,你有助于确认我对布尔运算的理解。例如除了 LHS 上的括号外,以下两个陈述是相同的。第一个评估为“假”,第二个评估为“真”。 >>> 假和真和真 == 假假 >>>(假和真和真) == 假真
      • 是的,优先级和括号在这里起了作用
      • “'==' 运算符优先且首先被评估”不,== 被最后且懒惰地评估。您缺少的是布尔运算符位于“顶部”(因为您通常希望 a == b and c == d 成为 (a == b) and (c == d) 而不是 a == (b and c) == d),因此 True and False and True == False 被解析为 (True) and (False) and (True == False),这显然是False x and False and x 必然是 False。
      • Masklinn - 所以声明 "True and True and False == False" 被分解成 1.)"True and True" , 2.) “假 == 假”1.), 2.) 先求值,再求 1.), 2.) 的结果为 "1.) 和 2.) " 返回 'True'?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-15
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多