1

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"

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

Comments

6

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

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.