Home > front end >  Trying to create an function which allows users to go back to a previous question
Trying to create an function which allows users to go back to a previous question

Time:11-19

so I'm making a text-based game in python and I'm trying to create an option which allows the user to return to the question if their answer was incorrect. It works like this:

There are three options to a question, 1,2,3. 1 and 3 are the incorrect option which will fail the user, then they will have the option to go back to the question. If a user picks 2 the first time then their answer is correct and they'll be allowed to proceed.

The problem is trying to create that option which allows them to go back.

I've tried using while loops and if statements. But none of those have worked. with the while loop, i expected that it would print the incorrect answers over and over again until the user picked the correct answer, but it printed the same answer even if the user picked something different.

and with if statements, I tried to create a variable which stored the user's input, and then used that variable in an if statement which made it so that when the user pressed "q" the question would print again.

I had hoped that when the question would print again, when the user picked a different option it would print the different answer assigned to it, but it instead printed nothing. When I type the incorrect answer, it prints the answer assigned to it, and gives me the option to press q, everything worked accordingly until that point, but when I typed the correct answer, it printed nothing, even though it should've printed a option.

Here's the code so you can a better understanding:

question_1 = input("You have three options 1) Try to break the window and escape. 2) Search the house for supplies. 3) Try to contact local authorities. Which do you choose?\n ")

if question_1 == "1":
        quit = input("You manage to break the window, but the glass cuts your hand and your face, you scream in pain and as the glass cuts through your face, and eventually die from the wounds. Press q to go back ")

  if quit == "q":
       second = input("You have three options 1) Try to break the window and escape. 2) Search the house for supplies. 3) Try to contact local authorities. Which do you choose?\n ")

if question_1 == "2":
        print("You find an array of supplies in your basement, everything from hammers, tools, weapons and food and water. You then safely break the window and jump out.")

That's the problem I'm facing, I'm not sure what's going on or what the fix is, so hope you all can help me.

Note: Also, it prints nothing IF only the incorrect option is chosen first, then the quit option. If I originally chose 2, then it prints the answer assigned to it with no problem.

CodePudding user response:

this maybe overkill but if it was me I would probably implement a scene graph... something like what follows

class Scene:
    graph_map = {}
    def __init__(self,id,message,options):
        self.message = message 
        self.opts = options
        self.id = id or len(Scene.graph)
        Scene.graph_map[id]=self

    def prompt(self):
        if self.opts:
            while True:
                resp = input(self.message)
                if resp in self.opts:
                   return Scene.graph_map[self.opts[resp]]
                print("ERROR Invalid input... try again")
        else:
            print(self.message)
            return None

    def play(self):
        target = self
        while True:
             target = target.prompt()
             if target and not isinstance(target,Scene):
                print("Error unknown scene...")
                return None

# make some scenes
welcome = Scene("welcome",message="""You have three options 1) Try to break the window and escape. 2) Search the house for supplies. 3) Try to contact local authorities. Which do you choose?\n """, options={"1":"window","2":"search","3":"help")
window = Scene("window",message="""You manage to break the window, but the glass cuts your hand and your face, you scream in pain and as the glass cuts through your face, and eventually die from the wounds. Press q to go back""", options={"q":"welcome"}
search = Scene("search",message="""You find an array of supplies in your basement, everything from hammers, tools, weapons and food and water. You then safely break the window and jump out.""",options=None)

#start the game
welcome.play()

CodePudding user response:

question_1=0

while question_1!="2":
    question_1 = input("You have three options 1) Try to break the window and escape. 2) Search the house for supplies. 3) Try to contact local authorities. Which do you choose?\n ")

    quit=0
    if question_1 == "1":

        while quit!= "q":
            quit = input("You manage to break the window, but the glass cuts your hand and your face, you scream in pain and as the glass cuts through your face, and eventually die from the wounds. Press q to go back ")

if question_1 == "2":
        print("You find an array of supplies in your basement, everything from hammers, tools, weapons and food and water. You then safely break the window and jump out.")

while question_1 is not 2, it keeps asking question 1.

If answer is 1, it keeps asking to quit until 'q' is pressed.

If answer is 2, question_1!="2" is now false so the while loop stops

  • Related