rolandvarga
rolandvarga

Reputation: 126

Python calling a function

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?

Upvotes: 1

Views: 297

Answers (1)

Waleed Khan
Waleed Khan

Reputation: 11467

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()

Upvotes: 1

Related Questions