Beginner question

Dave Angel davea at davea.name
Tue Aug 6 18:03:57 EDT 2013


eschneider92 at 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





More information about the Python-list mailing list