Home > Software engineering >  How do I write a question to play game again?
How do I write a question to play game again?

Time:02-05

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')

CodePudding user response:

Simply have a second loop that will continue to prompt until a valid input is given.

usr_response = input('Would you like to play again: ').lower()
while usr_response not in ('yes','no'):
    usr_response = input('Invalid response. Please choose yes or no: ')
if usr_response == 'no':
    break

CodePudding user response:

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!')

CodePudding user response:

You need to wrap your question into another while loop

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

    if outerbreak:
        break

  •  Tags:  
  • Related