永不结束的循环

huangapple go评论58阅读模式
英文:

Never ending loop

问题

循环应该在我输入YES或NO后结束,但它一直询问“ARE YOU DONE YES/NO”输入。以下是代码中的问题:

def Cable_df():
    YES = 'YES'
    NO = 'NO'
    v = ''
    while v != YES:
        x = input('Enter STARTING splice of cable').upper()
        y = input('Enter TERMINAL splice of cable ').upper()
        Z = input('Enter cable SIZE ').upper()
        v = input('ARE YOU DONE YES/NO ')
        while v != YES and v != NO:  # 修正此行
            print('Must answer YES/NO')
            v = input('ARE YOU DONE YES/NO ')

我修正了内部的while循环条件,将"or"改为"and",以确保只有在输入为YES或NO时才能结束循环。

英文:

the loop should end after I enter YES or NO but it keeps asking the 'ARE YOU DONE YES/NO' input
what is wrong with the following code

def Cable_df():
    YES = 'YES'
    NO = 'NO'
    v = ''
    while v != YES:
        x = input('Enter STARTING splice of cable').upper()
        y = input('Enter TERMINAL splice of cable ').upper()
        Z = input('Enter cable SIZE ').upper()
        v = input('ARE YOU DONE YES/NO ')
        while v != YES or v != NO:
            print('Must answer YES/NO')
            v = input('ARE YOU DONE YES/NO ')

答案1

得分: 3

以下是翻译好的部分:

问题的关键在于你在条件中使用的逻辑:

while v != YES or v != NO:
  • 如果v等于'YES',则v != YES的值为... ... 假,但是
  • 如果v等于'YES',则v != NO的值为... ... 真
  • 使用运算符将假 or 真的值变为真,循环无休止地运行。正确的输入也无法停止循环,因为'NO'也会返回真,因为'NO' != YES

换句话说,你需要改变while循环中的条件,使其对于正确的用户输入返回假。

由于'YES'和'NO'是接受的用户输入,这样的条件将是:

while not( (v == YES) or (v == NO) ):

上述while条件等同于使用Python关键字in的条件,它检查一个值是否在列表中出现:

while v not in [YES, NO]:

上面的条件规定的方式使代码看起来像口语英语,通常不需要解释它的功能,因为它会循环“当v不等于包含YES和NO的列表中的任何值时”。是否有优势取决于程序员的英语水平以及是否喜欢使用这种方式来编写和记录循环终止条件。

另一种陈述相同的方式是:

while v != YES and v != NO:

因为not (A or B)等于not A and not B

理解布尔逻辑的常见问题在于口语中使用词汇orand的方式。例如,如果有人问你:“你想要A还是B?”并且你认为你只能选择其中一个。布尔逻辑中的or也可以使用,如果你选择两者都可以。

英文:

The problem you face is the logic you use in the condition:

while v != YES or v != NO:
  • if v=='YES' then v != YES evaluates to ... ... False BUT
  • if v=='YES' then v != NO evaluates to ... ... True
  • the or makes from False or True the value True and the loop runs endlessly. A correct input does not stop the loop because also 'NO' will give True because 'NO' != YES.

In other words you need to change the condition in the while loop to another one which evaluates to False for correct user input.

As 'YES' and 'NO' are accepted user input such condition would be:

while not( (v == YES) or (v == NO) ):

The while condition above is equivalent to a condition using the Python keyword in which checks if a value occurs in a list:

while v not in [YES, NO]:

The above way of specifying a condition makes the code looks like spoken English and needs usually no explanation what it does because it will loop "while v is not equal to a value in the list containing YES and NO". If this is an advantage depends on how good the English the programmer speaks is and if it sounds appealing to him to use this way of writing and documenting a loop breaking condition.

Another way of stating the same would be:

while v != YES and v != NO:

because not ( A or B ) == not A and not B .

The common problem with understanding boolean logic is the way the words or and and are used in spoken language. For example if you are asked: "Do you want A or do you want B?" and you think you can choose only one of them. The boolean logical or would also be fine if you choose both.

答案2

得分: 2

在你的第二个循环中

while v != YES or v != NO:

你正在检查 v != YESv != NO。无论哪个条件满足,都足以满足检查并保持循环运行。所以它将继续,直到 v 同时等于 YESNO

可能你的意思是写

while v != YES and v != NO:

这将确保只有当 v != YES v != NO 时循环才会继续,因此一旦 vYESNO 时,循环将停止。

英文:

In your 2nd loop

while v != YES or v != NO:

you're checking if v != YES or v != NO. Either condition is enough to satisfy the check and keep the loop going. so it's going to continue until v is both YES and NO.

Possibly you meant to write

while v != YES and v != NO:

which would make sure the loop only keeps going if v != YES and v != NO, so the loop would stop once v is either YES or NO.

答案3

得分: 0

Use break to end a loop. e.g. while v == YES or v == No: break

英文:

Use break to end a loop. e.g. while v == YES or v == No: break

答案4

得分: 0

代码中的问题在于第二个while循环,因为条件"v != YES or v != NO"始终为True。要正确检查输入是否为"YES"或"NO",应该使用"v != YES and v != NO"。以下是已校正的代码:

def Cable_df():
    YES = 'YES'
    NO = 'NO'
    v = ''
    while v != YES:
        x = input('Enter STARTING splice of cable:').upper()
        y = input('Enter TERMINAL splice of cable:').upper()
        Z = input('Enter cable SIZE:').upper()
        v = input('ARE YOU DONE YES/NO:')
        while v != YES and v != NO:
            print('Must answer YES/NO:')
            v = input('ARE YOU DONE YES/NO:')

希望这对你有所帮助。

英文:

The issue is with the second while loop, as the condition "v != YES or v != NO" is always True. To correctly check if the input is either "YES" or "NO", you should use "v != YES and v != NO". Here's the corrected code:

def Cable_df():
    YES = 'YES'
    NO = 'NO'
    v = ''
    while v != YES:
        x = input('Enter STARTING splice of cable:').upper()
        y = input('Enter TERMINAL splice of cable:').upper()
        Z = input('Enter cable SIZE:').upper()
        v = input('ARE YOU DONE YES/NO:')
        while v != YES and v != NO:
            print('Must answer YES/NO:')
            v = input('ARE YOU DONE YES/NO:')

答案5

得分: 0

你必须满足所写的 while 循环继续的条件是:

v 不等于 "YES"
---- 或者 ----
v 不等于 "NO"

这两个条件将始终被检查,因此,因为 v 只能分配一个值 - 这两个选项中的一个始终为真

使用一个 IN 语句

def Cable_df():
    YES = 'YES'
    NO = 'NO'
    v = ''
    while v != YES:
        x = input('输入电缆的起始接头').upper()
        y = input('输入电缆的终端接头').upper()
        Z = input('输入电缆尺寸').upper()
        v = input('你完成了吗 YES/NO ')
        while v not in (YES, NO):
            print('必须回答 YES/NO')
            v = input('你完成了吗 YES/NO ')

更多参考信息:https://www.w3schools.com/python/ref_keyword_in.asp

英文:

The conditions you must have for the while loop you have written to continue are:<br>
v doesn't equal "YES"<br>
---- or ----<br>
v doesn't equal "NO"

Both conditions will always be checked and so, because v can only be assigned one value - one of these options will always be true

Use an IN statement

def Cable_df():
    YES = &#39;YES&#39;
    NO = &#39;NO&#39;
    v = &#39;&#39;
    while v != YES:
        x = input(&#39;Enter STARTING splice of cable&#39;).upper()
        y = input(&#39;Enter TERMINAL splice of cable &#39;).upper()
        Z = input(&#39;Enter cable SIZE &#39;).upper()
        v = input(&#39;ARE YOU DONE YES/NO &#39;)
        while v not in (YES, NO):
            print(&#39;Must answer YES/NO&#39;)
            v = input(&#39;ARE YOU DONE YES/NO &#39;)

Further reference: https://www.w3schools.com/python/ref_keyword_in.asp

huangapple
  • 本文由 发表于 2023年2月6日 10:43:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定