Reputation: 2433
I am tryign to check if any of the values "GIN" or "NOT READY" or "TO BE ABANDON OR NEEDS RESUBMISSION" equals retrunVal,i notice that for any returnVal the "if" loop" is getting and "INSIDE" is getting printed,I am doubting the syntax is not right,can anyone provide inputs?
if ('GIN' or 'NOT READY' or 'TO BE ABANDON OR NEEDS RESUBMISSION' == returnVal):
print "INSIDE"
Upvotes: 1
Views: 143
Reputation: 11038
Your code, logically, reads like this:
if 'GIN' exists
or if 'NOT READY' exists
or if 'TO BE ABANDON OR NEEDS RESUBMISSION' is equal to retVal
do something
Read this link about truth-values in python (this is also related to paxdiablo's answer).
a better approach is to use python's "in" statement:
if retVal in ['GIN', 'NOT READY', 'TO BE ABANDON OR NEEDS RESUBMISSION']:
do something
Upvotes: 6
Reputation: 881123
This is one way to do it:
if (returnVal == 'GIN') or (returnVal == 'NOT READY') or returnVal == '...':
though a more Pythonic better way would be to use in
:
if returnVal in ['GIN', 'NOT READY', '...']:
In other words (for that first case), use separate conditions and just or
them together.
The reason you're always seeing INSIDE
is because 'GIN'
is a effectively being treated as a true
value in the context of a conditional:
>>> if 'GIN':
... print "yes"
...
yes
and true or <anything>
is true
.
Upvotes: 2
Reputation: 235984
Like this:
if returnValue in ('GIN', 'NOT READY', 'TO BE ABANDON OR NEEDS RESUBMISSION'):
print 'INSIDE'
That's the standard idiom - using the in
operator to test membership in a tuple with all possible values. Much cleaner than a bunch of or
'ed contitions.
Upvotes: 8