1

I am trying to do one of the exercises from learn python the hard way and am stuck on something. I created a function and in case one of the statements is fulfilled I would like to put that in another function. This is the outline of how I'm trying to do it:

def room_1():
    print "Room 1"
    button_push = False

    while True:
       next = raw_input("> ")

        if next == "1":
            print "You hear a click but nothing seems to happen."
            button_push = True

        elif next == "2":
            print "You enter room 2"
            room_2()

def room_2():
    print "Room 2"

    while True:
        next =raw_input("> ")

        if next == "1" and button_push:
            print "You can enter Room 3"
            room_3()

If button_push is fulfilled then I would like to see that in room_2. Could anyone please help me with that?

1 Answer 1

1

You can pass button_push as an argument to the next room:

def room_1():
    print "Room 1"
    button_push = False

    while True:
       next = raw_input("> ")

        if next == "1":
            print "You hear a click but nothing seems to happen."
            button_push = True

        elif next == "2":
            print "You enter room 2"
            room_2(button_push)  # pass button_push as argument

def room_2(button_push):  # Accept button_push as argument
    print "Room 2"

    while True:
        next =raw_input("> ")

        if next == "1" and button_push:  # button_push is now visible from this scope
            print "You can enter Room 3"
            room_3()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the prompt response! Is there a way to pass button_push only if I input "1" first and "2" only after?
@user2714517 You're passing True if they inputted 1 and then 2 or False otherwise. If you need to know whether 1 then 2 was pressed, you can just check for the value of button_push. Since you have and button_push, you'll only enter room 3 if they inputted 1 then 2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.