# Newbie question related to Boolean in Python

S

#### skwyang93

1. bear_moved = False
2.
3. while True:
4. next = raw_input("> ")
5.
6. if next == "take honey":
8. elif next == "taunt bear" and not bear_moved:
9. print "The bear has moved from the door. You can go through."
10.
11. bear_moved = True
12. elif next == "taunt bear" and bear_moved:
14. elif next == "open door" and bear_moved:
15. gold_room()
16. else:
17. print "I got no idea what that means.

# This is just to show my understanding of Boolean. In line 8-9, if my input is "taunt bear", the result is true and true, which will continue the loop.

# So what confused me is line 12-13. if my input is taunt bear, is it suppose to be taunt bear == "taunt bear" and bear_moved which is true and true? which means the loop will continue instead of cancelling it.

Source: Learnpythonthehardway

N

#### Neil Cerutti

1. bear_moved = False
2.
3. while True:
4. next = raw_input("> ")
5.
6. if next == "take honey":
8. elif next == "taunt bear" and not bear_moved:
9. print "The bear has moved from the door. You can go through."
10.
11. bear_moved = True
12. elif next == "taunt bear" and bear_moved:
14. elif next == "open door" and bear_moved:
15. gold_room()
16. else:
17. print "I got no idea what that means.

# This is just to show my understanding of Boolean. In line 8-9, if my input is "taunt bear", the result is true and true, which will continue the loop.

# So what confused me is line 12-13. if my input is taunt bear, is it suppose to be taunt bear == "taunt bear" and bear_moved which is true and true? which means the loop will continue instead of cancelling it.

Your logic looks OK, but the indentation on your code is screwy.
It should not compile like that. There may be indentation errors,
but I don't want to make assumptions when the indentation is
definitely not what your real code says. Can you cut and paste

D

#### Dave Angel

1. bear_moved = False
2.
3. while True:
4. next = raw_input("> ")
5.
6. if next == "take honey":
8. elif next == "taunt bear" and not bear_moved:
9. print "The bear has moved from the door. You can go through."
10.
11. bear_moved = True
12. elif next == "taunt bear" and bear_moved:
14. elif next == "open door" and bear_moved:
15. gold_room()
16. else:
17. print "I got no idea what that means.

Please indent by 4, not 2 characters. It's very hard to see what's
lined up with what. And that's compounded by having the line numbers
there so that the first 9 lines are shifted left.
# This is just to show my understanding of Boolean. In line 8-9, if my input is "taunt bear", the result is true and true, which will continue the loop.

Those lines have compound if conditions. Line 8 will be true/true
only the first time you type "taunt bear". Notice the operator "not" in
front of bear_moved.
# So what confused me is line 12-13. if my input is taunt bear, is it suppose to be taunt bear == "taunt bear" and bear_moved which is true and true? which means the loop will continue instead of cancelling it.

Line 12 will be true/true only if you've already run line 11. Since
bear_moved = False initially, the only way you get true /true here is by

T

#### Thomas Yang

bear_moved = False

while True:
next = raw_input("> ")

if next == "take honey":
elif next == "taunt bear" and not bear_moved:
print "The bear has moved from the door. You can go through."
bear_moved = True
elif next == "taunt bear" and bear_moved:
elif next == "open door" and bear_moved:
gold_room()
else:
print "I got no idea what that means."

# sorry if it looks confusing, this is the real code

S

#### Steven D'Aprano

bear_moved = False

while True:
next = raw_input("> ")

if next == "take honey":
elif next == "taunt bear" and not bear_moved:
print "The bear has moved from the door. You can go
through."
bear_moved = True
elif next == "taunt bear" and bear_moved:
elif next == "open door" and bear_moved:
gold_room()
else:
print "I got no idea what that means."

# sorry if it looks confusing, this is the real code

No it isn't, because it gives an indent error. The first line is
unindented, the second line is indented. One of those must be wrong.

py> bear_moved = False
py> while True:
File "<stdin>", line 1
while True:
^
IndentationError: unexpected indent

Please be careful of indentation. Python's indentation rules are great
for code, but not so great for copying and pasting into emails and
websites. So beware, and take extra care with indentation.

# So what confused me is line 12-13. if my input is taunt bear, is it
suppose to be taunt bear == "taunt bear" and bear_moved which is true
and true? which means the loop will continue instead of cancelling it.

The relevant two lines are:
elif next == "taunt bear" and bear_moved:

I don't understand your question. Nothing cancels the loop anywhere.
Presumably the loop repeats forever, unless the function dead() contains
a sys.exit or similar.

The first time around the loop, the bear has not moved, and presumably is
blocking the door. So the variable `bear_moved` is false. If the user's
command is "taunt bear" (the poorly named variable `next`) then the first
time around the loop this if statement will trigger:

if next == "taunt bear" and not bear_moved:

a message will print stating that the bear moves away from the door, and
`bear_moved` will be set to True. At that point, the *entire*
if...elif...else block is finished, execution will jump to the end of the
loop, and the next loop will begin.

The second time through the loop, the user is prompted for a command
again. If the user enters "taunt bear" a second time, then

if next == "taunt bear" and bear_moved:

is triggered, since this time `bear_moved` is True, and the bear eats the
character.