0

Here is a loop to make sure players of my small game input valid answers but the while loop conintues infinitely, even when the conditions are satisfied to break the loop:

initialize = ''
    while initialize != 'yes' or initialize != 'no':
        print('\nWould you like to play?')
        initialize = input('Yes or No? ').lower().strip()
        if initialize == 'yes':
            print('\nHappy to see you.\n')
        elif initialize == 'no':
            print('\nNo worries.\n\nThanks for stopping by!') 
        else:
            print('\nSorry, I didn\'t catch that')
print('please wait as game loads')
print('\nYou\'re very important to us\n')    

if 'yes' or 'no' is typed in the correct if statement runs but the loop continues: Would you like to play? Yes or No? yes

Happy to see you.

Would you like to play? Yes or No?

  • Your while condition is equivalent to `not (initialize == 'yes' and initialize == 'no')` ([De Morgan's Law](https://stackoverflow.com/a/2168630/4859885)). In this form, it's clear to see why it loops indefinitely. – Brian Rodriguez Nov 29 '20 at 14:19
  • Related:[multiple conditions with while loop in python](https://stackoverflow.com/questions/26578277/multiple-conditions-with-while-loop-in-python). – wwii Nov 29 '20 at 14:48

2 Answers2

4

You don't want or, you want and, since it will always be unequal to one of the two:

initialize = 'yes' # start it off with something valid so the loop runs
while initialize != 'yes' and initialize != 'no':
    print('\nWould you like to play?')
    initialize = input('Yes or No? ').lower().strip()
    if initialize == 'yes':
        print('\nHappy to see you.\n')
    elif initialize == 'no':
        print('\nNo worries.\n\nThanks for stopping by!') 
    else:
        print('\nSorry, I didn\'t catch that')

Alternatively, use not in:

while initialize not in ['yes', 'no']:
    # code goes here
Aplet123
  • 33,825
  • 1
  • 29
  • 55
0

Very quick and dirty hack

initialize = ''


while initialize == '':
    print('\nWould you like to play?')
    initialize = input('Yes or No? ').lower().strip()
    if initialize == 'yes':
        print('\nHappy to see you.\n')
    elif initialize == 'no':
        print('\nNo worries.\n\nThanks for stopping by!')
    else:
        print('\nSorry, I didn\'t catch that')
        
print('please wait as game loads')
print('\nYou\'re very important to us\n') 

The problem with your code was that the evaluation in the while bit is wrong.

Joel Chu
  • 828
  • 1
  • 9
  • 25