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 ')
CodePudding user response:
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.
CodePudding user response:
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
.
CodePudding user response:
Use break to end a loop. e.g. while v == YES or v == No: break
CodePudding user response:
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:')
CodePudding user response:
The conditions you must have for the while loop you have written to continue are:
v doesn't equal "YES"
---- or ----
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 = '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 not in (YES, NO):
print('Must answer YES/NO')
v = input('ARE YOU DONE YES/NO ')
Further reference: https://www.w3schools.com/python/ref_keyword_in.asp