0

I'm working on a text-based game in Python and basically it's a series of functions that store match/case statements where the player can type things in to select different choices, which then bring the player to different functions. The problem is that a simple typo can quit the game or jump to a random function. The simple solution to this would be the example code I have pasted below, where a case_: statement catches the unrecognized inputs, but I have over 100 functions and I was hoping I would not have to add this manually for each one.

Is there any way I could have some globally defined property where if an input is not recognized from the options given (i.e., the other cases), the user is taken back to the start of that function where they can try again?

Example code:

def examplechoice():
    response = input("What is your favorite season?\n")
    match (response.lower()):
        case "winter":
            print("some function here")
        case "spring":
            print("some function here")
        case "summer":
            print("some function here")
        case "fall":
            print("some function here")
        case _:
            print("response not recognized--try again")
            examplechoice()
Nolan__
  • 3
  • 3
  • First off, calling a function from within itself **is not** "taking the user back to the start of that function". It is **the same thing as calling any other function**: which is to say, there is a **completely new** context for the function call, and when that call completes, it will `return` a value to the place where it was called. – Karl Knechtel Feb 28 '23 at 06:15
  • The answer to the question as asked is a simple "no", but I doubt that this is the real question you have, or that it is useful to tell you this. It comes across that you are probably writing much more code than the problem demands, and that you do not have good strategies or techniques for *organizing* the code. This is an extremely common problem for new programmers, who commonly want to create games like what is described here. However, we don't really do tutorials here; we need a more specific question to have something actionable. – Karl Knechtel Feb 28 '23 at 06:18
  • All of that said, consider whether [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658) answers the real, underlying question. – Karl Knechtel Feb 28 '23 at 06:19

1 Answers1

1

You can use a simple while True loop with break. The loop will go on forever until you enter the right season. Just need a little modification to your code.

def examplechoice():
    while True:
        response = input("What is your favorite season?\n")
        match (response.lower()):
            case "winter":
                print("some function here")
                break
            case "spring":
                print("some function here")
                break
            case "summer":
                print("some function here")
                break
            case "fall":
                print("some function here")
                break
            case _:
                print("response not recognized--try again")

examplechoice()
Ted Nguyen
  • 66
  • 4