May-26-2021, 12:43 AM
You could either loop here until you get an answer you want, or you might prefer to push it off to another function. If you will have several locations in the code, I'd much rather have it in a separate function.
Then you could call it like:
def get_specific_input(prompt, valid_responses): response = input(prompt) while response not in valid_responses: print(f"Please enter only one of {','.join(valid_responses)}." response = input(prompt) return responseHere the while loop will continue to prompt until the response matches one of the valid responses. Since it has already done the checking, your main code can just assume that one of the expected choices will be present.
Then you could call it like:
if choice=='s': print("ok you've choosen the sword. You see two doors. one is to the left and one is to the right") doors=get_specific_input('Which door do you want to open? l or r ', ['l', 'r']) if doors =='r':