Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #52067 > unrolled thread
| Started by | eschneider92@comcast.net |
|---|---|
| First post | 2013-08-06 14:35 -0700 |
| Last post | 2013-08-06 20:06 -0700 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.python
Beginner question eschneider92@comcast.net - 2013-08-06 14:35 -0700
Re: Beginner question Dave Angel <davea@davea.name> - 2013-08-06 22:03 +0000
Re: Beginner question Chris Angelico <rosuav@gmail.com> - 2013-08-06 23:10 +0100
Re: Beginner question "Rhodri James" <rhodri@wildebst.demon.co.uk> - 2013-08-06 23:14 +0100
Re: Beginner question Chris Down <chris@chrisdown.name> - 2013-08-06 23:46 +0200
Re: Beginner question eschneider92@comcast.net - 2013-08-06 20:06 -0700
| From | eschneider92@comcast.net |
|---|---|
| Date | 2013-08-06 14:35 -0700 |
| Subject | Beginner question |
| Message-ID | <6e80b2f8-0b14-43cd-b8af-211ef65d73ba@googlegroups.com> |
Why won't the 'goodbye' part of this code work right? it prints 'ok' no matter what is typed. Much thanks.
def thing():
print('go again?')
goagain=input()
if goagain=='y' or 'yes':
print('ok')
elif goagain!='y' or 'yes':
print('goodbye')
sys.exit()
thing()
[toc] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-08-06 22:03 +0000 |
| Message-ID | <mailman.273.1375826661.1251.python-list@python.org> |
| In reply to | #52067 |
eschneider92@comcast.net wrote:
> Why won't the 'goodbye' part of this code work right? it prints 'ok' no matter what is typed. Much thanks.
>
> def thing():
> print('go again?')
> goagain=input()
> if goagain=='y' or 'yes':
This expression doesn't do what you think. The comparison binds more
tightly, so it first evaluates (goagain=="y"). The results of that are
either True or False. Then it or's that logical value with 'yes'. The
result is either True or it's 'yes'. 'yes' is considered truthy, so
the if will always succeed.
What you meant to use was:
if goagain == "y" or goagain == "yes":
Alternatively, you could use
if goagain in ("y", "yes"):
> print('ok')
> elif goagain!='y' or 'yes':
Same here.
> print('goodbye')
> sys.exit()
> thing()
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-08-06 23:10 +0100 |
| Message-ID | <mailman.274.1375827040.1251.python-list@python.org> |
| In reply to | #52067 |
On Tue, Aug 6, 2013 at 10:35 PM, <eschneider92@comcast.net> wrote:
> Why won't the 'goodbye' part of this code work right? it prints 'ok' no matter what is typed. Much thanks.
>
> def thing():
> print('go again?')
> goagain=input()
> if goagain=='y' or 'yes':
> print('ok')
> elif goagain!='y' or 'yes':
> print('goodbye')
> sys.exit()
> thing()
When you use 'or' in this way, it's not doing what you think it does.
It actually first computes
>>> goagain=='y'
which is either True or False, and then evaluates the next part:
>>> True or 'yes'
>>> False or 'yes'
Try out those two in interactive Python and see what they do. You
probably want to compare in both halves of the test, or possibly use
the 'in' operator:
if goagain in ('y', 'yes'):
Also, you don't need an 'elif' there; just use a plain else. Repeating
the condition just introduces the chance of bugs - for instance, would
you notice the error in this?
if goagain=='y' or goagain=='yes':
print('ok')
elif goagain!='y' or goagain!='yes':
print('goodbye')
Using a plain 'else' guarantees that exactly one of the branches will
be executed, rather than having the possibility that neither will,
which isn't the intention here.
Hope that helps!
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | "Rhodri James" <rhodri@wildebst.demon.co.uk> |
|---|---|
| Date | 2013-08-06 23:14 +0100 |
| Message-ID | <op.w1e1eniha8ncjz@gnudebeest> |
| In reply to | #52067 |
On Tue, 06 Aug 2013 22:35:44 +0100, <eschneider92@comcast.net> wrote:
> Why won't the 'goodbye' part of this code work right? it prints 'ok' no
> matter what is typed. Much thanks.
>
> def thing():
> print('go again?')
> goagain=input()
> if goagain=='y' or 'yes':
This line doesn't do what you think it does :-) Typing that condition at
an interactive prompt reveals something interesting:
>>> goagain = "n"
>>> goagain=="y"
False
>>> goagain=="y" or "yes"
'yes'
Oho. What's actually happening is that you've mistaken the operator
precedence. "==" has a higher precedence than "or", so your condition is
equivalent to '(goagain=="y") or "yes"'. Since it's left-hand argument is
False, "or" returns its right-hand argument, which has the value 'yes',
which in a boolean context is "True".
What you seem to want to do is to have your condition be true if goagain
is either "y" or "yes". Probably the easiest way of doing this and
learning something new at the same time is to ask if goagain appears in a
list (or rather a tuple) of strings:
if goagain in ('y', 'yes'):
print('ok')
elif goagain not in ('y', 'yes'):
print('goodbye')
sys.exit()
or better,
if goagain in ('y', 'yes', 'ohdeargodyes', 'you get the idea'):
print('ok')
else:
print('goodbye')
sys.exit()
--
Rhodri James *-* Wildebeest Herder to the Masses
[toc] | [prev] | [next] | [standalone]
| From | Chris Down <chris@chrisdown.name> |
|---|---|
| Date | 2013-08-06 23:46 +0200 |
| Message-ID | <mailman.284.1375832602.1251.python-list@python.org> |
| In reply to | #52067 |
[Multipart message — attachments visible in raw view] — view raw
On 2013-08-06 14:35, eschneider92@comcast.net wrote: > Why won't the 'goodbye' part of this code work right? it prints 'ok' no > matter what is typed. Much thanks. "if" statements do not fall through, because the first statement was matched, no other ones in the same chain will be evaluted. "elif" means "else if", where "else" means "if nothing previous matched".
[toc] | [prev] | [next] | [standalone]
| From | eschneider92@comcast.net |
|---|---|
| Date | 2013-08-06 20:06 -0700 |
| Message-ID | <d777ca25-f918-4162-b1ad-90171e28610f@googlegroups.com> |
| In reply to | #52067 |
Thanks that helped a lot!
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web