0

I have imported sys and created a "play again" function that is called correctly and outputs to the screen. However, the function executes os.execl(sys.executable, sys.executable, *sys.argv) no matter what input it is given.

def playAgain():
     print("would you like to play again? Y/N :")
     playAgainx  = input()
     if playAgainx == "Y" or "y" or "yes" or "Yes" or "YES":
          os.execl(sys.executable, sys.executable, *sys.argv)
     elif playAgainx == "n" or "N":
          sys.exit()
     else:
         print("invalid input")
         playAgain()
blau
  • 73
  • 3
  • 13
  • 1
    ```if playAgainx == "Y" or "y" or "yes" or "Yes" or "YES":``` this doesn't do what you think it does. - ```if playAgainx in ["yes, "y", "YES"]``` will do what you want. – Joshua Nixon Mar 13 '20 at 17:37
  • 1
    In Python ```if playAgainx == "Y" or "y" or "yes" or "Yes" or "YES":``` will always evaluate to True because non-empty string in Python evaluates to True. To do what you intended you need to add ```playAgainx == ``` in front of all your options...So like ```playAgainx == "Y" or playAgainx == "y" or playAgainx == "yes"..etc. – JYCH Mar 13 '20 at 17:38
  • 1
    Also, using the string method `lower` will reduce the number of cases to test to two. – Jan Christoph Terasa Mar 13 '20 at 17:39
  • You need to provide the statement for all of it's occurences: If playAgainx == "Y" or playAgainx == "y" ... And so on. Problem is: the boolean value of the converted "yes" is true, therefore at least one of the statements in the "if" is true and it enters it. – Roy Levy Mar 13 '20 at 17:41

1 Answers1

1

Use

if playAgainx.lower() in ["y","yes"]:

instead of

if playAgainx == "Y" or "y" or "yes" or "Yes" or "YES":

In this way the if statement will be true in case of y or yes (indifferent of its case)

santo
  • 418
  • 1
  • 3
  • 13