Sean Valdivia Sean Valdivia - 3 months ago 8
Python Question

Why doesn't the bear(boolean) remain in its position?

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.

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.

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.

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.

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?

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("> ")
if next.isdigit():
if int(next) > 101:
dead("You greedy bastard!")
elif int(next) < 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("> ")
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("> ").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("> ").lower()

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

start()

Answer

Here's the top of your bear_room function, with the global functionality fixed. You still have to appropriately alter the calls to the routine.

You are still learning about Boolean values; note the changes I made in your tests. You don't have to test bear_moved == True; the variable itself is a True/False value. Comparing it against True does nothing. Comparing it against False is simply the not operation. I deleted your else: pass because the only way to reach that spot is if bear_moved is NaN (not a number), a value you shouldn't see in this program. Logically, you couldn't get to that clause at all.

Of course, there are still improvements to make in your program: fix the spelling and grammar errors, make the logic flow a little more cleanly (did you do any sort of flow chart for this?), and nest if statements to save work and reading time.

def bear_room():
    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?"
    else:
        print "The bear has moved away from the door."