pythonapplication-restart

How do I restart the main() function in my python script? The program restarts no matter what input I add


How do I ensure that when "Y" "y" "yes" is input the program restarts? Then If anything else is input then the program ends? At the moment if anything is input (including nothing at all) and I press Enter, the program will restart regardless.

The code I have written to do this is below, it is all wrapped within a main() function:

def main():

# Main program code here

# Ask the player whether they want to retry
    if display == wordChosen:
      tryAgain = str(input("Try again: (Y = yes / N = no)"))
      tryAgain = tryAgain.lower()
      if tryAgain == "Y" or "y" or "yes":
        print(tryAgain) # for testing just to see whether its being picked up
        main()
      else:
        exit

main()

Solution

  • You can change your if statement to this: if tryAgain == "y" or tryAgain == "yes": since you already converted tryAgain input to lowercase, no need to check if its "Y" since it will never be uppercased. And the line if tryAgain == "Y" or "y" or "yes": Checks if tryAgain is "Y" and then checks if "y" is true or "yes" is true, not if tryAgain is "y" or "yes", and as far as I know non-empty strings are always true.