-1

I am trying to restart my program in python, but, for some reason, the code is getting caught up in the while playagain loop. It says "That was not a valid answer. Please enter 'yes' or 'no'." when I enter yes, no, or any of the other inputs that are acceptable. Why won't this work?

while True:
    playagain = input("Would you like to play again?")
    while playagain != "y" or playagain != "Y" or playagain != "Yes" or playagain != "yes" or playagain != "YES" or playagain != "n" or playagain != "N" or playagain != "No" or playagain != "no" or playagain != "NO":
         print("That was not a valid answer. Please enter 'yes' or 'no'.")
         playagain = input("Would you like to play again?")
    if playagain == "y" or playagain == "Y" or playagain == "Yes" or playagain == "yes" or playagain == "YES":
         break
    else:
         print("Thank you for playing MadLibs!")

3 Answers3

3

You can use the lower() function and the in operator to clean up the code.

You would change this code:

playagain = input("Would you like to play again?").lower()
while playagain not in ['y', 'yes', 'n', 'no']:
     print("That was not a valid answer. Please enter 'yes' or 'no'.")
     playagain = input("Would you like to play again?").lower()

So that the whole code would be:

while True:
    playagain = input("Would you like to play again?").lower()
    while playagain not in ['y', 'yes', 'n', 'no']:
         print("That was not a valid answer. Please enter 'yes' or 'no'.")
         playagain = input("Would you like to play again?").lower()
    if playagain in ['n', 'no']:
         print("Thank you for playing MadLibs!")
         break
lmjohns3
  • 6,991
  • 5
  • 32
  • 54
perreal
  • 90,214
  • 20
  • 145
  • 172
  • 1
    I might even be lazier and say `input(...).lower().startswith('y')` – Nick T Oct 06 '14 at 04:48
  • Ok, I did a version of this and it helped, but now instead of starting over when someone says yes and ending when someone says no, the message thank you for playing is printed and then the program starts over when you say yes and no – xnathanmeyerx Oct 06 '14 at 04:52
  • i think it has something to do with this part of the code : if playagain.lower() in ['y', 'yes']: break else: print("Thank you for playing MadLibs!") – xnathanmeyerx Oct 06 '14 at 04:53
  • @xnathanmeyerx, try the whole code from the answer. – perreal Oct 06 '14 at 04:56
  • ok, that worked, but why didnt the one that i just put above work? it looks the same to me except you used no instead of yes and you got rid of the else – xnathanmeyerx Oct 06 '14 at 05:01
  • @xnathanmeyerx, you should break when the users says no. But your main problem is using `or` instead of `and`. – perreal Oct 06 '14 at 05:02
  • @xnathanmeyerx take a look at my answer - it addresses your real problem. – Wyetro Oct 06 '14 at 05:21
0

You want:

while playagain != "y" or playagain != "Y" or playagain != "Yes"

to be:

while playagain != "y" and playagain != "Y" and playagain != "Yes"
Wyetro
  • 8,262
  • 9
  • 46
  • 63
0

You can also do like this:

while True:
    valid_response = ["y","Yes","Y","No","n","N"]
    positive_response = ["y","Yes","Y"]
    playagain = input("Would you like to play again?")
    while playagain not in valid_response:
        print("That was not a valid answer. Please enter 'yes' or 'no'.")
        playagain = input("Would you like to play again?")
    if playagain in positive_response:
        printf("You chose to play again"):
        break
    else:
        print("Thank you for playing MadLibs!")
        break;

The problem in your code is that at least one of ther statements in your while loop be true always. Because playagain can not be equal to "Y", "y", "Yes", "No", "n" and "N" at the same time (For the condition in loop to be false).

Ashwani
  • 1,858
  • 9
  • 15