-1

The if/else statement inside of this loop will not work. If I enter "no" it still continues the loop. I don't understand why, any help would be appreciated.

while keepPlaying == True:
    play = input(str('Would you like to play rock, paper, scissors? Yes or no?\n'))

    if play == str('yes') or str('Yes'):
        playerChoice = input(str('Ok! what\'s your choice?\n'))
    else:
        keepPlaying = False

print ('Thanks for playing.')

I've put the code through a visualizer and even if the variable play != yes or Yes it still chooses the == to path.

Michael Porter
  • 229
  • 3
  • 12

2 Answers2

4

The problem is this:

if play == str('yes') or str('Yes'):

It is equivalent to:

if (play == str('yes')) or str('Yes'):

Note that str('Yes') is always truthy (and with an or means you'll never get the false part).

You want:

if play in ('yes', 'Yes'):

Or maybe (just a tip):

if play.lower() == 'yes':
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
0

Change the if condition to play in ('Yes', 'yes', 'y', 'Y')

The issue is, the second part of the or clause (str('Yes')) always evaluates to true.

Srini
  • 1,626
  • 2
  • 15
  • 25