【问题标题】:Python 'while' with two conditions: "and" or "or"Python 'while' 有两个条件:“and”或“or”
【发布时间】:2019-06-07 08:35:39
【问题描述】:

这是一个非常简单的掷骰子程序,它不断掷出两个骰子,直到得到双六。所以我的 while 语句结构如下:

while DieOne != 6 and DieTwo != 6:

由于某种原因,程序在DieOne 得到一个 6 时立即结束。 DieTwo 根本不考虑。

但是,如果我在 while 语句中将 and 更改为 or,则程序可以完美运行。这对我来说没有意义。

import random
print('How many times before double 6s?')
num=0
DieOne = 0
DieTwo = 0

while DieOne != 6 or DieTwo != 6:
    num = num + 1
    DieOne = random.randint(1,6)
    DieTwo = random.randint(1,6)
    print(DieOne)
    print(DieTwo)
    print()
    if (DieOne == 6) and (DieTwo == 6):
        num = str(num)
        print('You got double 6s in ' + num + ' tries!')
        print()
        break

【问题讨论】:

  • 如果 DieOne 是 6,那么声明 DieOne != 6 and DieTwo != 6 是错误的,因为这不是真的,两个骰子都不等于 6。
  • 这对我来说很有意义,所以不清楚你在问什么。您希望循环在 两个 检查都为假时结束,所以 or 是正确的组合。
  • 问题是这样的。你想要NOT(die 1 is 6 AND die 2 is 6). 等价的条件变成die 1 is NOT 6 OR die 2 is NOT 6. 这是一个你需要解决的逻辑问题。当您说“die 1 is not 6 AND die 2 is not 6”时,当其中一个变为 6 时,条件将立即失败,因为 AND 需要确保满足两个条件。
  • 简化:while DieOne+DieTwo != 12: ... 或简单地将while True: 与您的break 一起使用

标签: python while-loop boolean multiple-conditions


【解决方案1】:

TLDR 在底部。

首先,如果以下条件为真,while 循环就会运行,所以

DieOne != 6 or DieTwo != 6:

简化时必须返回 true,才能运行 while 函数

and 运算符在 两个 条件都为真时返回真,所以 while 循环只会在 真和真 时运行。

因此,例如,如果任一骰子掷出 6,则以下内容将不会运行:

while DieOne != 6 and DieTwo != 6:

如果 DieOne 掷出 4 而 DieTwo 掷出 6,则 while 循环不会运行,因为 DieOne != 6 为真,而 DieTwo != 6 为假。我把这个思路放到了下面的代码中。

while DieOne != 6 and DieTwo != 6:
while True and False:
while False: #So it won't run because it is false

or 运算符的工作方式不同,or 运算符在 其中一个 条件为 true 时返回 true,因此 while 循环将在以下情况下运行它是 True 或 TrueTrue 或 False、或 _False 或 True。 所以

while DieOne != 6 or DieTwo != 6:

只要有一个骰子掷出 6,就会运行。例如:

如果 DieOne 掷出 4 而 DieTwo 掷出 6,则 while 循环将运行,因为 DieOne != 6 为真,而 DieTwo != 6 为假。我把这个思路放到了下面的代码中。

while DieOne != 6 or DieTwo != 6:
while True or False:
while True: #So it will run because it is true

TLDR/审查:

while True: #Will run
while False: #Won't run

还有:

while True and True: #Will run
while True and False: #Won't run
while False and True: #Won't run
while False and False: #Won't run

或者:

while True or True: #Will run
while True or False: #Will run
while False or True: #Will run
while False or False: #Won't run

【讨论】:

    【解决方案2】:

    您需要的是Not 而不是!=

    试试这个:

    while not (DieOne == 6 or DieTwo == 6):
    

    【讨论】:

    • 很好的尝试,但这与我的原始代码遇到了同样的问题。如果将 Or 运算符更改为 And,它就可以工作。
    【解决方案3】:
    while DieOne != 6:
       if DieTwo != 6:
          break
       num = num + 1
       DieOne = random.randint(1, 6)
       DieTwo = random.randint(1, 6)
       print(DieOne)
       print(DieTwo)
       print()
       if (DieOne == 6) and (DieTwo == 6):
          num = str(num)
          print('You got double 6s in ' + num + ' tries!')
          print()
          break
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。您可以在帮助中心找到更多关于如何写好答案的信息:stackoverflow.com/help/how-to-answer。祝你好运?
    猜你喜欢
    • 2017-07-29
    • 2011-07-24
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多