Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52068

Re: Beginner question

From Dave Angel <davea@davea.name>
Subject Re: Beginner question
Date 2013-08-06 22:03 +0000
References <6e80b2f8-0b14-43cd-b8af-211ef65d73ba@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.273.1375826661.1251.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web