FAQ
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()

Search Discussions

  • Chris Down at Aug 6, 2013 at 9:46 pm

    On 2013-08-06 14:35, 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.

    "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".
    -------------- next part --------------
    A non-text attachment was scrubbed...
    Name: not available
    Type: application/pgp-signature
    Size: 490 bytes
    Desc: not available
    URL: <http://mail.python.org/pipermail/python-list/attachments/20130806/387b87f5/attachment.pgp>
  • Dave Angel at Aug 6, 2013 at 10:03 pm

    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
  • Chris Angelico at Aug 6, 2013 at 10:10 pm

    On Tue, Aug 6, 2013 at 10:35 PM, 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
  • Rhodri James at Aug 6, 2013 at 10:14 pm

    On Tue, 06 Aug 2013 22:35:44 +0100, 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
  • Eschneider92 at Aug 7, 2013 at 3:06 am
    Thanks that helped a lot!

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 6, '13 at 9:35p
activeAug 7, '13 at 3:06a
posts6
users5
websitepython.org

People

Translate

site design / logo © 2021 Grokbase