Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53738 > unrolled thread
| Started by | skwyang93@gmail.com |
|---|---|
| First post | 2013-09-05 13:08 -0700 |
| Last post | 2013-09-05 21:02 -0700 |
| Articles | 6 — 6 participants |
Back to article view | Back to comp.lang.python
Newbie question related to Boolean in Python skwyang93@gmail.com - 2013-09-05 13:08 -0700
Re: Newbie question related to Boolean in Python Neil Cerutti <neilc@norwich.edu> - 2013-09-05 20:38 +0000
Re: Newbie question related to Boolean in Python Thomas Yang <skwyang93@gmail.com> - 2013-09-05 16:36 -0700
Re: Newbie question related to Boolean in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-06 02:28 +0000
Re: Newbie question related to Boolean in Python Dave Angel <davea@davea.name> - 2013-09-05 20:47 +0000
Re: Newbie question related to Boolean in Python Tim Roberts <timr@probo.com> - 2013-09-05 21:02 -0700
| From | skwyang93@gmail.com |
|---|---|
| Date | 2013-09-05 13:08 -0700 |
| Subject | Newbie question related to Boolean in Python |
| Message-ID | <f2259124-4af1-4608-9969-8e42a140633e@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Date | 2013-09-05 20:38 +0000 |
| Message-ID | <b8s8e6F1ll0U1@mid.individual.net> |
| In reply to | #53738 |
On 2013-09-05, skwyang93@gmail.com <skwyang93@gmail.com> wrote:
> 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?
--
Neil Cerutti
[toc] | [prev] | [next] | [standalone]
| From | Thomas Yang <skwyang93@gmail.com> |
|---|---|
| Date | 2013-09-05 16:36 -0700 |
| Message-ID | <dd0aa339-d713-4368-996f-77cc0b6e3929@googlegroups.com> |
| In reply to | #53740 |
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
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-09-06 02:28 +0000 |
| Message-ID | <52293db6$0$29988$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #53746 |
On Thu, 05 Sep 2013 16:36:55 -0700, Thomas Yang wrote:
> 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?
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-09-05 20:47 +0000 |
| Message-ID | <mailman.106.1378414084.5461.python-list@python.org> |
| In reply to | #53738 |
On 5/9/2013 16:08, skwyang93@gmail.com wrote:
> 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.
>
> Thanks in advance for spending your time to answer my question.
> Source: Learnpythonthehardway
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Tim Roberts <timr@probo.com> |
|---|---|
| Date | 2013-09-05 21:02 -0700 |
| Message-ID | <ipki299eaas78n6dqdi3uj75i1p8pg46sb@4ax.com> |
| In reply to | #53738 |
skwyang93@gmail.com wrote: > ># 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. I'm not quite sure what you're expecting. There is nothing in any of the cases that will exit the loop. The loop is infinite. I assume (with no evidence) that the "dead" function calls sys.exit, which kills the program. In that case, this makes a little bit of sense. The first time you type "taunt bear", bear_moved is set True and the loop runs again. If you type "taunt bear" a second time, then the bear is pissed off and you call dead(). -- Tim Roberts, timr@probo.com Providenza & Boekelheide, Inc.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web