-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!")
  • The negation of `a or b` is `(not a) and (not b)`. – Willem Van Onsem Oct 06 '14 at 04:40
  • 1
    Please do not repost closed questions. If your question is closed, edit it to make it worth reopening. – JasonMArcher Oct 08 '14 at 23:12
  • Reopening this. Originally, OP had asked a different version of the question that **had a different problem**, and it was closed as a duplicate as appropriate. OP then tried to edit the question to show that the supposed fix still resulted in non-working code. Finally, this question is a duplicate **of the edited code**, but the code in the other question **should not have been edited like that** in the first place, because it makes a completely different question. I.e., this one. Which is therefore **not** a duplicate. – Karl Knechtel Feb 03 '23 at 11:26
  • After reading the answers (and looking more closely at the code), I determined that the problem that was surfaced here is in fact **also** a common duplicate - of a different question. So I have used that instead. – Karl Knechtel Feb 03 '23 at 11:34

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
  • 7,422
  • 5
  • 36
  • 56
perreal
  • 94,503
  • 21
  • 155
  • 181
  • 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,439
  • 9
  • 46
  • 64
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,938
  • 11
  • 15