If statements with OR in then problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PythonNotSoGuru
    New Member
    • Oct 2007
    • 21

    If statements with OR in then problems

    Hey everyone i am a newbie to python so could someone help me figure out why this code keeps returning "how are you" no matter what i input for x. Im trying to minimalize the amount of elif statements in the program by combining them with the OR inbetween.

    print 'hello'
    x = raw_input ('')




    if x=='hi' or 'hey' or 'hello' or 'whats up' or 'wats up' or 'watsup' or 'sup':
    print 'how are you'

    elif x=='who are you' or 'what are you' or 'whats your name' or 'wats your name':
    print 'my name is compy'
    else:
    print 'i dont understand your gibberish'

    THANX EVERYONE
    ps the lines after the if elif and else statements are all indented in my actual code i just couldnt get them to indent on this posting.
  • elcron
    New Member
    • Sep 2007
    • 43

    #2
    Originally posted by PythonNotSoGuru
    Hey everyone i am a newbie to python so could someone help me figure out why this code keeps returning "how are you" no matter what i input for x. Im trying to minimalize the amount of elif statements in the program by combining them with the OR inbetween.

    print 'hello'
    x = raw_input ('')




    if x=='hi' or 'hey' or 'hello' or 'whats up' or 'wats up' or 'watsup' or 'sup':
    print 'how are you'

    elif x=='who are you' or 'what are you' or 'whats your name' or 'wats your name':
    print 'my name is compy'
    else:
    print 'i dont understand your gibberish'

    THANX EVERYONE
    ps the lines after the if elif and else statements are all indented in my actual code i just couldnt get them to indent on this posting.
    In python the "or" operator separates statements in python these two statements would be equivalent:
    [code=python]
    if x=='hi' or 'hey' or 'hello' or 'whats up' or 'wats up' or 'watsup' or 'sup':
    # and
    if (x=='hi') or ('hey') or ('hello') or ('whats up') or ('wats up') or ('watsup') or ('sup'):
    [/code]
    and any non-empty string evaluates as True. I would use the "in" keyword to fix it. Though you may want to replace the lists with variables to increase readability.

    [code=python]
    print 'hello'
    x = raw_input ('')

    if x in ['hi', 'hey', 'hello', 'whats up', 'wats up', 'watsup', 'sup']:
    print 'how are you'

    elif x in ['who are you', 'what are you', 'whats your name', 'wats your name']:
    print 'my name is compy'

    else:
    print 'i dont understand your gibberish'
    [/code]

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      My friend, elcron, has a great suggestion. I prefer to use lists and the in operator for readability and maintainability , but to show your error, I'll post this correction:[CODE=python]if x == 'hi' or x == 'hey':[/CODE]

      Please note the use of [CODE] tags, here. Instructions are on the right hand side of the page while you are posting.

      Comment

      • PythonNotSoGuru
        New Member
        • Oct 2007
        • 21

        #4
        Ok thank you everybody for the help! My new code works great now. Thanks alot.

        PS Sorry for the lack of my [code] thing will comply next time.

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by PythonNotSoGuru
          Ok thank you everybody for the help! My new code works great now. Thanks alot.
          You are quite welcome.
          PS Sorry for the lack of my [code] thing will comply next time.
          That's no problem. I don't start getting cranky about this until about the third offense (Hence, the "please").

          Comment

          Working...