0

My python code has some errors in the try- except loop. If you enter an input which is not yes or no, it first prints the "yes" reply output, then once you answer the question it shows the output for if you do not enter yes or no. Here's the code:

playAgain = None

while playAgain != "Yes" or "No" or "yes" or "no" or "y" or "n":
    try:
            playAgain = str(input("Do you want to play again? Enter Yes or No: "))         
            if playAgain == "Yes" or "y" or "yes":
                displayIntro()
            elif playAgain == "No" or "no" or "n":
                print("Oh, well. The magic 8 ball will see you next time...")
                sys.exit()
    except:
                print("That wasn't yes or no, idiot. The magic 8 ball will not give a fortune to such an imbocile.")

Please help and thank you!

  • `try` `except` is not a loop. `except` catches errors which happen in the `try`. If no error happens, you'll never get to the `except`. – pythomatic Nov 27 '18 at 22:35

1 Answers1

4
playAgain != "Yes" or "No" or "yes" or "no" or "y" or "n"

Is not a correct way to do this.

When you say playAgain != "Yes" then you need to do the same for the remaining expression. So a valid way to do what you were intended to do is the following:

playAgain != "Yes" or playAgain != "No" or playAgain != "yes" or playAgain != "no" or playAgain != "y" or playAgain != "n"

But this is ugly and too long.

Instead, use

playAgain not in ["Yes", "No", "yes", "no", "y", "n"]

In Python, we have some handy ways to deal with such problems. You can use the in operator to check if the string in question exists (or does not exist) in a list of possible values. It's also very nice to read: "if playAgain (is) not in [this list of values]".

You can even manipulate the input so it's easier for you to work with. That is, you lower all the letters and you don't check for case-sensitive input (if you really don't care about case sensitive input; do you really care if is Yes or yEs?):

playAgain.lower() not in ["yes", "y"]

Something like this should do:

while True:
    playAgain = str(input("Do you want to play again? Enter Yes or No: "))         

    if playAgain.lower() in ["yes", "y"]:
        # do something with your yes input. Consider `break` out of the endless loop.
    elif playAgain.lower() in ["no", "n"]:
        # do something with your no input. Consider `break` out of the endless loop.
    else:
        print("That wasn't yes or no.")

Note that the above loop is endless. You need to break-out according to your program logic. That is, you need to put a break statement somewhere when you need to break out of the endless loop.

Rafael
  • 7,002
  • 5
  • 43
  • 52
  • Thanks for the help. This code has been edited: – PyhtonErrorAssistance Nov 29 '18 at 06:16
  • playAgain = None while playAgain not in ["Yes", "No", "yes", "no", "y", "n"]: try: playAgain = str(input("Do you want to play again? Enter Yes or No: ")) if playAgain == ["Yes", "y", "yes"]: displayIntro() elif playAgain == ["No" "no", "n"]: print("Oh, well. The magic 8 ball will see you next time...") sys.exit() except: print("That wasn't yes or no, idiot. The magic 8 ball will not give a fortune to such an imbocile.") – PyhtonErrorAssistance Nov 29 '18 at 06:17
  • However, this still does not work. – PyhtonErrorAssistance Nov 29 '18 at 06:17
  • I have provided a full example that works. Your code is not working because you still have `if playAgain == ["Yes", "y", "yes"]` where it should be: `if playAgain in ["Yes", "y", "yes"]`. You have the same error in two places. – Rafael Nov 29 '18 at 08:35