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":
7. dead("The bear looks at you then slaps your face off.")
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:
13. dead("The bear gets pissed off and chews your leg off.")
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.

Thanks in advance for spending your time to answer my question.
Source: Learnpythonthehardway
 
Ad

Advertisements

N

Neil Cerutti

1. bear_moved = False
2.
3. while True:
4. next = raw_input("> ")
5.
6. if next == "take honey":
7. dead("The bear looks at you then slaps your face off.")
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:
13. dead("The bear gets pissed off and chews your leg off.")
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.

Thanks in advance for spending your time to answer my question.

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
your code directly instead of retyping it?
 
D

Dave Angel

1. bear_moved = False
2.
3. while True:
4. next = raw_input("> ")
5.
6. if next == "take honey":
7. dead("The bear looks at you then slaps your face off.")
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:
13. dead("The bear gets pissed off and chews your leg off.")
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
answering "taunt bear" twice.
 
T

Thomas Yang

bear_moved = False

while True:
next = raw_input("> ")

if next == "take honey":
dead("The bear looks at you then slaps your face off.")
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:
dead("The bear gets pissed off and chews your leg off.")
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
 
Ad

Advertisements

S

Steven D'Aprano

bear_moved = False

while True:
next = raw_input("> ")

if next == "take honey":
dead("The bear looks at you then slaps your face off.")
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:
dead("The bear gets pissed off and chews your leg off.")
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.

Copied from your previous message, your question was:
# 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:
dead("The bear gets pissed off and chews your leg off.")


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.


Does this answer your question?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top