简体   繁体   中英

If the User inputs the incorrect answer in Python, how can I make the program ask the question again?

I am making a text adventure code for my computer science class. My code works well, but when the program asks the user if they want to play again, if they don't enter something starting with "y" or "n" then the program automatically restarts. I want it to ask the question again instead so that the user can enter the correct input. I put a # near that part of the code.

school_map = """This is the map of your school.
-------------------------------------------
| classroom A | classroom B | classroom C |
|______ ______|_______ _____|______ ______|
|                                         |
|-------   ---------|                     |
|                   |---------   ---------|
|     cafeteria     |                     |
|                   |                     |
|___________________|         gym         |
|                   |                     |
|   locker rooms                          |
|___________________|_____________________|
|              ☘️             🪨      🍁  |
|      🍄           garden       🌸      |
|  🍀        🥀             🌷           |
-------------------------------------------
"""

def get_choice(prompt) -> int:
    while True:
        choice = input(prompt).lower()
        if choice == "x":
            print(school_map)
        elif choice in "12":
            return int(choice)
        else:
            print("Value must be 1 or 2.")


while True:
    print("WELCOME TO LIVRE DE LA MORT.")
    print("*.*.*.*.*.*.*.*.*.*.*.*.*.*.")
    print(" ")
    name = input("What is your name? ")
    print(f"You are a genius high school senior, {name}, whose goal in life is to bring justice to the world.")
    print(school_map)
    print("If you want to see the map again, input 'X' at any given time.")
    print("You are skipping gym class in the locker rooms and looking out a window when you see a notebook fall from the sky.")
    if get_choice(
        "You can either: 1 - go and pick it up, or 2 - ignore it: "
    ) == 1:
        print("You pick up the notebook and find instructions written on the inside.")
        print("WELCOME.")
        print("I AM THE LIVRE DE LA MORT.")
        print("SINCE YOU HAVE PICKED ME UP, YOU ARE THE CHOSEN ONE.")
        print("IF YOU WRITE ANY PERSON'S NAME IN THIS BOOK, AS LONG AS YOU KNOW THEIR FACE, THEY WILL DIE WITHIN THE NEXT 5 MINUTES.")
        if get_choice(
            "WILL YOU: 1 - JOIN ME IN THIS BATTLE FOR JUSTICE, or 2 - PUT ME BACK DOWN AND HAVE YOUR MEMORY WIPED? "
        ) == 1:
            print("Good. We shall work together.")
            break
        else:
            print("Your memory has been wiped.")
    else:
        print("You graduate high school and work a 8-6 job for the rest of your life as a police officer. ")
#what if user presses the wrong key?
    playagain = input("Would you like to play again? ").lower()
    if playagain.startswith("y"):
        print("ok.")
        print(" ")
        print("*.*.*.*.*.*.*.*.*.*.*.*.*.*.")
    elif playagain.startswith("n"):
        print("You are the weak link. Goodbye.")
        exit()

You can change that part of the code with:

# Rest of the code
# .
# .
# .
  playagain = input("Would you like to play again? ")
  while playagain:
    if playagain.startswith("y"):
        print("ok.")
        print(" ")
        print("*.*.*.*.*.*.*.*.*.*.*.*.*.*.")
        break
    elif playagain.startswith("n"):
        print("You are the weak link. Goodbye.")
        exit()
    else:
      playagain = input("Would you like to play again? ")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM