Why doesn't the bear(boolean) remain in its position?
<p>I wanted to add more functionality to exercise 35 of Zed Shaw's LPTHW. The script runs without crashing and allows the player to revisit previous rooms, as I desired. However, in the bear_room the only way the player can get through is to scream at the bear, switching the bear_moved boolean to True.</p>

<p>If the player does this and then goes backward into the start room, it was my intent that the bear_moved boolean remained in the True position, meaning that the bear would still be moved away from the door upon re-entry. </p>

<p>That isn't happening when I run the script; Upon entering the bear_room for the first time I scream at the bear, causing it to move away from the door. I then back out of the room, returning to the start room. When I go back into the bear_room the bear has mysteriously plopped its fat self in front of the door again.</p>

<p>I placed the bear_moved boolean outside of the function just for this purpose--this was the only thing I could come up with to give that extra functionality to the program. </p>

<p>To recap, why doesn't the bear stay moved when I exit the bear_room and re-enter? How can I achieve the functionality I'm aiming for?</p>

<pre><code>from sys import exit
bear_moved = False

def gold_room():
    print "This room is full of gold. How much do you take?"
    next = raw_input("&gt; ")
    if next.isdigit():
        if int(next) &gt; 101:
            dead("You greedy bastard!")
        elif int(next) &lt; 101:
            print "Nice, you're not greedy! You win!"
            exit(0)
        else:
            pass
    elif 'back' in next or 'backtrack' in next:
        bear_room(bear_moved)
    else:
        print "Type a number!"
        gold_room()

def bear_room(bear_moved):
    if bear_moved == False:
        print "There's a bear in here."
        print "The bear has a bunch of honey."
        print "The fat bear is in front of another door."
        print "How are you going to move the bear?"
    elif bear_moved == True:
        print "The bear has moved away from the door."
    else:
        pass

    next = raw_input("&gt; ")
    if 'honey' in next:
        dead("The looks at you and then slaps your face off.")
    elif 'taunt' in next or 'tease' in next or 'scream' in next or 'yell' in next and bear_moved == False:
        bear_moved = True
        bear_room(bear_moved)
    elif 'taunt' in next or 'tease' in next or 'scream' in next or 'yell' in next and bear_moved == True:
        dead("The bear flies into a rage and chews your leg off.")
    elif 'open' in next and bear_moved == True:
        gold_room()
    elif 'open' in next and bear_moved == False:
        dead("The bear, still standing in the way, lunges up and rips your throat out.")
    elif 'back' in next or 'backtrack' in next:
        start()
    else:
        print "I got no idea what this means."
        bear_room(bear_moved)

def cthulhu_room():
    print "Here you see the great evil Cthulhu."
    print "He, it, whatever stares at you and you go insane."
    print "Do you flee for your life or eat your head?"

    next = raw_input("&gt; ").lower()

    if 'flee' in next:
        start()
    elif 'head' in next:
        dead("Well that was tasy!")
    else:
        cthuhlu_room()

def dead(why):
    print why, "Game Over!"
    exit(0)

def start():
    print "You're in a dark room."
    print "You have no idea who you are or how you got here."
    print "Your head is throbbing..."
    print "There are two doors, one to your left, and one to your right."
    print "Which door do you take?"

    next = raw_input("&gt; ").lower()

    if 'left' in next:
        bear_room(bear_moved)
    elif 'right' in next:
        cthulhu_room()
    else:
        start()

start()
</code></pre>

<p>I researched even more about Python's global and local variables. I now realize that I was changing a local variable within the function, not the global bear_moved. Which is exactly why the bear had plopped its fat self back in front of the door upon re-entry.</p>

<p>After stripping the parameter from the bear_room() function and making the bear_moved reference inside of it global, the function worked just as I had originally wanted it to. I also replaced the bear_moved == (boolean) expressions with simply bear_moved or not bear_moved.</p>

<pre><code>from sys import exit
bear_moved = False

# Exercise 35 of Zed Shaw's LPTHW
# Player can now revisit previous rooms
# The bear_room function uses simple recursion 
# instead of the while loop of the original script


def gold_room():
    print "This room is full of gold. How much do you take?"
    next = raw_input("&gt; ")
    if next.isdigit():
        if int(next) &gt; 101:
            dead("You greedy bastard!")
        elif int(next) &lt; 101:
            print "Nice, you're not greedy! You win!"
            exit(0)
        else:
            pass
    elif 'back' in next or 'backtrack' in next:
        bear_room()
    else:
        print "Type a number!"
        gold_room()

def bear_room():

    """Globalizing bear_moved variable
       and stripping parameter as advised."""

    global bear_moved
    if not bear_moved:
        print "There's a bear in here."
        print "The bear has a bunch of honey."
        print "The fat bear is in front of another door."
        print "How are you going to move the bear?"
    elif bear_moved:
        print "The bear has moved away from the door."


    next = raw_input("&gt; ")
    if 'honey' in next:
        dead("The looks at you and then slaps your face off.")
    elif 'taunt' in next and not bear_moved:
        bear_moved = True
        bear_room()
    elif 'taunt' in next and bear_moved:
        dead("The bear flies into a rage and chews your leg off.")
    elif 'open' in next and bear_moved:
        gold_room()
    elif 'open' in next and not bear_moved:
        dead("The bear, still standing in the way, lunges up and rips your throat out.")
    elif 'back' in next or 'backtrack' in next:
        start()
    else:
        print "I got no idea what this means."
        bear_room()

def cthulhu_room():
    print "Here you see the great evil Cthulhu."
    print "He, it, whatever stares at you and you go insane."
    print "Do you flee for your life or eat your head?"

    next = raw_input("&gt; ").lower()

    if 'flee' in next or 'run' in next:
        start()
    elif 'head' in next:
        dead("Well that was tasy!")
    else:
        cthulhu_room()

def dead(why):
    print why, "Game Over!"
    exit(0)

def start():
    print "You're in a dark room."
    print "You have no idea who you are or how you got here."
    print "Your head is throbbing..."
    print "There are two doors, one to your left, and one to your right."
    print "Which door do you take?"

    next = raw_input("&gt; ").lower()

    if 'left' in next:
        bear_room()
    elif 'right' in next:
        cthulhu_room()
    else:
        start()

start()
</code></pre>

python recursion boolean global functionality