473,394 Members | 1,944 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,394 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 1234
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.