473,236 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,236 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 1232
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

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

Similar topics

11
by: dmbkiwi | last post by:
I am new to this group, and relatively new to python programming, however, have encountered a problem I just cannot solve through reading the documentation, and searching this group on google. I...
7
by: Matthew Wilson | last post by:
Hi- I'm writing a bunch of classes, several of which need functions and variables defined in the math module. In some instances, I'm going to import my module like this: import myshapes ...
1
by: ST | last post by:
Hi, I'm trying to debug someone else's code, and I'm going thru this Select Case statement. I'm having problems with the "OTHER" case...in that when the first line of the case is false, it jumps...
1
by: Tom D | last post by:
I'm rewriting a database interface that our company currently has. Currently it's using the Pear::DB interface, but we found that that was introducing a bit too much overhead. I'm rewriting the...
4
by: DG | last post by:
Hi, Can anyone advise how to execute multiple statements in a single query batch. For example- update customers set customer_name = 'Smith' where customer_name = 'Smyth'; select * from...
6
by: flaavia | last post by:
Hello My oleDbDataAdapter1 seems to be limited to 99 variables in SQL-statements. At least it does give an error when tying to address more than 99 Does anyboda know if that is a fixed limit...
34
by: Jeff | last post by:
For years I have been using VBA extensively for updating data to tables after processing. By this I mean if I had to do some intensive processing that resulted in data in temp tables, I would have...
12
by: Jerim79 | last post by:
I have created a verification script to verify information and redirect the customer to the appropriate error page. For example: if ($FName=""){ header('Location:/verify_fname.htm'); } else{...
17
by: Navodit | last post by:
So I have some code like: if (document.Insurance.State.selectedIndex == 1) { ifIll(); } else if (document.Insurance.State.selectedIndex == 2) { elseKan(); }
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.