Exercise 35: bear_moved?

Hi fellow pythonists,

I have a question re LPTHW ex35 - there is a bear_moved = False in the sample code but I don’t understand why it is there and what’s the purpose… It seems only line 17 (elif choice == “taunt bear” and not bear_moved:) will run, the other elif on line 21 will never get hit - is that correct? So that’s the purpose of putting it there?

Thanks in advance.

Cheers, Wes

I attached the codes here for easy reference:

def bear_room():
    print("""
    There is a bear here.
    The bear has a bunch of honey.
    The fat bear is in front of another door.
    How are you going to move the bear?
    """)
 _# ?? What does bear_move mean and what's its purpose ??_
    bear_moved = False

    while True:
        choice = input("> ")

        if choice == "take honey":
            dead("The bear looks at you then salps your face off.")
        elif choice == "taunt bear" and not bear_moved:
            print("There bear has moved from the door.")
            print("You can go through it now.")
# ?? Does 'bear_moved = True' below run if ' elif choice == "taunt bear" and not bear_moved:' didn't run ??
            bear_moved = True
        elif choice == "taunt bear" and bear_moved:
            dead("The bear gets pissed off and chews your leg off.")
        elif choice == "open door" and bear_moved:
            gold_room()
        else:
            print("I got no idea what that means.")

@Elrond

I haven’t got to EX35 yet just on EX34 but looking at the code it looks to me.
‘bear_moved = False’ initializes the Boolean flag to a known and desired state.

Then to get through the door you have to first ‘taunt bear’ which will set ‘bear moved’ flag from False to True then you can choose open door to get to the gold room.
Effectively you have to play the game in this order to get ‘bear_moved = ‘ flag from False to True’, the bear is now out of the way, you can now choose ‘open door’ and it will execute the 3rd elif statement to get to the gold_room()

am I making sense, is this helping?

1 Like

What you should do is print out the value of bear_moved everywhere you see it being used and then run this a few times to see what’s going on. Something like:

print(">>> bear_moved=", bear_moved)

or

print(">>> before while: bear_moved=", bear_moved)

Then you’ll see how it’s changing and understand what it does. Another way to think about it is, when you enter the room, the bear is sitting there right? So it has NOT moved. That means it starts out as false.

1 Like

In python exercise 35, line 34 will consider bear_moved from line 24 or line 33? the code seems to be using bear_moved from line 33.

Am I right in assuming, the output of an if/ elif code does not affect the subsequent elif code?

The only time a variable changes is when you use the equal, of if you use the . on it.

With the = you’re changing it like this

bear_moved = True

or

bear_moved = False

The other way you’ll learn about later but that’s when you do something like myfile.close() which will change it.

What this means for you is, the code is using the “current value”, and that’s whatever assignment most recently ran. So, you have to print the variable before and after each = usage to see what’s going on. Each place you see bear_moved = False or bear_moved = True you’ll want to trace it with print like I have above. Then follow along in the code to see what’s going on.

I actually do this a lot in the videos.

Here is what i did. Instead of “not bear_moved” or “bear_moved”, I change to arguement “True” or “False”. I am almost sure they do the same thing, but “True” or “False” arguements help me unerstand better. I hope this helps.

1 Like

Thanks Victor for helping me understand this exercise, I was about to give up. Effectively: “and not bear_moved” is just equal too: " and bear_moved is False".

A free service run by Zed A. Shaw for learncodethehardway.org.