lundi 7 septembre 2020

Dealing with non- "yes or no" inputs [duplicate]

The following code prints user's input(it asks for their name) and prints it. It continues to run until the user answers no.

continueNaming = True
# While continueNaming is true...ran the program
while continueNaming :
    #User inputs their first & last name
    firstName = input("What's your first name?")
    lastName = input("What's your last name(s)?")
    # logs it
    print(f"{lastName}, {firstName}")
    
    # Ask if they want to continue
    noOrYes = input(f"Do you want to continue? (Y/N)")
    
    # If yes or any other programmed equivalent, "continueNaming = True",
    # which contines the program
    if noOrYes == "Y" or noOrYes == "y":
        continueNaming = True
        
    # If no or any other programmed equivalent, "continueNaming = false",
    # ending the program   
    elif noOrYes == "N" or noOrYes == "n":
        continueNaming = False
        
    elif noOrYes != "Y" or "N":
        noOrYes = input(f"That's not an answer, do you want to continue? (Y/N)")

The problem arises when the user types anything that not "Y" or "N". If they do so, it resets and asks for their name again. I would like to repeatedly ask the user to use "Y" or "N" until they do so. I realized that changing finishName to false would end it.

Aucun commentaire:

Enregistrer un commentaire