0

How do i error trap for other letters in a play again menu with python. i would like to ask the user if they "want to play again? Y/N" using try and except.

def playAgain():
answer = input("Play again? Y/N: ")
while answer == Y: 
    main () 
while answer == N: 
    break
martineau
  • 119,623
  • 25
  • 170
  • 301
user1998
  • 1
  • 1

1 Answers1

1

You can use a while loop to keep asking the user for a valid input until the user does so. Make the playAgain() function return a Boolean value instead so that the actual control logic can be made in the main program instead:

def playAgain():
    while True:
        answer = input("Play again? Y/N: ").lower()
        if answer in 'yn':
            break
    return answer == 'y'

def main():
    while True:
        # main code
        if not playAgain():
            break
blhsing
  • 91,368
  • 6
  • 71
  • 106