471,330 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

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.
Oct 8 '07 #1
4 1058
elcron
43
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:
Expand|Select|Wrap|Line Numbers
  1. if x=='hi' or 'hey' or 'hello' or 'whats up' or 'wats up' or 'watsup' or 'sup':
  2. # and
  3. if (x=='hi') or ('hey') or ('hello') or ('whats up') or ('wats up') or ('watsup') or ('sup'):
  4.  
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.

Expand|Select|Wrap|Line Numbers
  1. print 'hello'
  2. x = raw_input ('')
  3.  
  4. if x in ['hi', 'hey', 'hello', 'whats up', 'wats up', 'watsup', 'sup']:
  5.     print 'how are you'
  6.  
  7. elif x in ['who are you', 'what are you', 'whats your name', 'wats your name']:
  8.     print 'my name is compy'
  9.  
  10. else:
  11.     print 'i dont understand your gibberish'
  12.  
Oct 8 '07 #2
bartonc
6,596 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. if x == 'hi' or x == 'hey':
Please note the use of [code] tags, here. Instructions are on the right hand side of the page while you are posting.
Oct 8 '07 #3
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.
Oct 8 '07 #4
bartonc
6,596 Expert 4TB
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").
Oct 9 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by dmbkiwi | last post: by
7 posts views Thread by Matthew Wilson | last post: by
1 post views Thread by ST | last post: by
12 posts views Thread by Jerim79 | last post: by
17 posts views Thread by Navodit | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.