Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loop adventure game
#2
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.

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 response
Here 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':
jefsummers likes this post
Reply


Messages In This Thread
loop adventure game - by ilikedofs - May-25-2021, 11:52 PM
RE: loop adventure game - by bowlofred - May-26-2021, 12:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  While loop not ending (Best of 10 dice game) K3nidi 3 2,331 Jul-09-2022, 09:53 AM
Last Post: K3nidi
  For Loop and Use of Brackets to Modify Dictionary in Tic-Tac-Toe Game new_coder_231013 7 3,704 Dec-28-2021, 11:32 AM
Last Post: new_coder_231013
  how to make a hotkey for text adventure game myn2018 2 2,633 Jan-06-2021, 10:39 PM
Last Post: myn2018
  Choose your own adventure game noahc2004 2 3,395 Jun-26-2020, 02:06 PM
Last Post: DPaul
  Designing a "game" loop api pitosalas 2 3,288 Jun-07-2020, 03:20 PM
Last Post: pitosalas
  Waiting in a text adventure StickyLizard 1 57,741 Jan-19-2019, 10:45 PM
Last Post: ichabod801
  Errors in simple text adventure game? ecloev 5 6,585 Apr-10-2018, 05:55 PM
Last Post: nilamo
  loop doesn't advance-py3 text game foxtreat 2 4,268 Jun-08-2017, 06:41 AM
Last Post: foxtreat
  Need a little more help in a Choose Your Own Adventure Program Goldberg291 13 24,206 Jan-31-2017, 08:33 AM
Last Post: Ofnuts
  Python Text Adventure sneakyninja0129 2 32,103 Jan-01-2017, 09:18 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020