'How do I write a question to play game again?

I'm expanding upon the random number guessing game from Automate the Boring Stuff with Python and I can't figure out how to write the code for when the game asks if you want to play again.

More specifically, if the user types "yes" or "no" to wanting to play the game, the code does the appropriate thing. However, if the user types in anything else, I want it to say "Please answer yes or no" and then allow the user to enter another answer.

In this case, my code currently prints "Please answer yes or no", but then it treats the answer as "yes", so it starts the game again. I don't want it to immediately start a new game unless the user specifically types "yes". How can I do this?

Here's the code

import random

print ('Hello, what is your name?')
name = input ()

name = name.strip()

while True:

    print ('Well, ' + name + ', I am thinking of a number between 1 and 20.')
    secretNumber = random.randint (1, 20)

    print ('DEBUG: Secret number is ' + str(secretNumber))

    print ('Take a guess.') 


    for guessesTaken in range (1, 7):
        try: 
            guess  = int (input ())
        
            if (guess < secretNumber and guessesTaken < 6):
                print ('Your guess is too low. Guess again.')
            elif (guess > secretNumber and guessesTaken < 6):
                print ('Your guess is too high. Guess again.')
            else:
                break # This condition is for the correct guess

        except ValueError:
            print ('You did not enter a number.') # This condition is for if a non-integer is entered.

    if (guess == secretNumber and guessesTaken == 1):
        print ('Good job, ' + name + '! You guessed my number in ' + str(guessesTaken) + ' guess.')
         
    elif (guess == secretNumber and guessesTaken > 1): 
        print ('Good job, ' + name + '! You guessed my number in ' + str(guessesTaken) + ' guesses.')
        
    else:
        print ('Sorry. Your guesses were all wrong. The number I was thinking of was ' + str(secretNumber))

    print ('Would you like to play again?') 

    answer = input ()
    answer = answer.lower()
    
    if answer == 'no': 
        print ('Ok.')
        break 
    elif answer == 'yes':
        print ('Ok, let\'s go!') 
    else: 
        print ('Please answer yes or no')


Solution 1:[1]

So the problem is that the code checks for valid input only once. If the 2nd input is invalid as well, no condition is checked. For the solution, You should replace:

if answer == 'no': 
    print ('Ok.')
    break 
elif answer == 'yes':
    print ('Ok, let\'s go!') 
else: 
    print ('Please answer yes or no')

with:

list1 = ['yes', 'no']
while answer not in list1:
    print ('Please answer yes or no:')
    answer = input()
if answer == 'no': 
    print ('Ok.')
    break 
elif answer == 'yes':
    print ('Ok, let\'s go!')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1