0
player = input('do you wanna play the game').lower()

while player != 'yes' or 'no':

    player = input('say yes or no')

if player == 'yes':
   print('ok')
else:
    quit()

in this code even when we type yes or no it doesn't respond to it and just continues the while loop i don't know what is wrong

I was expecting to when the user enters yes or no just jump to the if else statment otherwise just stuck in loop

  • 2
    `player != 'yes' or 'no'` will always be true – kuro Jun 09 '23 at 10:40
  • `If` is not valid Python syntax. Are you just typing code in the question without checking whether it parses? – trincot Jun 09 '23 at 10:44
  • "or" is going to return one or the other. `player != 'yes'` if that evaluates to false, then `'no'` is returned. It's not clear how you want them to evaluate though. Maybe you want something like `while player not in ["yes", "no"]:` – matt Jun 09 '23 at 10:46

3 Answers3

0

Try this

player = ''
print('do you want to play the game')
while player != 'yes' and player != 'no':
    player = input('say yes or no ').lower()

if player == 'yes':
   print('ok')
else:
    quit()
Pragmatic_Lee
  • 473
  • 1
  • 4
  • 10
0

The issue with your code is in the while loop condition. The condition player != 'yes' or 'no' is not being evaluated as you expect. It is actually being evaluated as (player != 'yes') or ('no'). Since 'no' is a non-empty string, it is considered truthy, and the condition will always be true.

To fix this issue, you should change the condition to:


    while player != 'yes' and player != 'no':

Here's the corrected code:


    player = input('do you wanna play the game').lower()
    
    while player != 'yes' and player != 'no':
        player = input('say yes or no')
    
    if player == 'yes':
        print('ok')
    else:
        quit()

Now, the while loop will continue until the user inputs either 'yes' or 'no', and then it will proceed to the if-else statement.

0

The condition of the while is incorrect, you can pick any from the following for the intended usage:

for player in ["yes", "no", "other"]:
    print()
    print(f"{player=}")
    print(f"{player != 'yes' and player != 'no'      = }")
    print(f"{not (player == 'yes' or player == 'no') = }")
    print(f"{player not in ['yes', 'no']             = }")
    print(f"{not player in ['yes', 'no']             = }")

which prints:

player='yes'
player != 'yes' and player != 'no'      = False
not (player == 'yes' or player == 'no') = False
player not in ['yes', 'no']             = False
not player in ['yes', 'no']             = False

player='no'
player != 'yes' and player != 'no'      = False
not (player == 'yes' or player == 'no') = False
player not in ['yes', 'no']             = False
not player in ['yes', 'no']             = False

player='other'
player != 'yes' and player != 'no'      = True
not (player == 'yes' or player == 'no') = True
player not in ['yes', 'no']             = True
not player in ['yes', 'no']             = True
paime
  • 2,901
  • 1
  • 6
  • 17