2013-06-28 54 views
1

我試圖檢查是否任何值「GIN」或「不準備」或「要放棄或需要RESUBMISSION」等於retrunVal,我注意到對於任何returnVal「if」循環「越來越和‘內部’是越來越印,我懷疑的語法是不正確的,任何人都可以提供輸入如何檢查多個返回值

if ('GIN' or 'NOT READY' or 'TO BE ABANDON OR NEEDS RESUBMISSION' == returnVal): 
     print "INSIDE" 

回答

8

像這樣:

if returnValue in ('GIN', 'NOT READY', 'TO BE ABANDON OR NEEDS RESUBMISSION'): 
    print 'INSIDE' 

這是標準的成語 - 使用in?運算符來測試具有所有可能值的元組中的成員資格,比一堆01更清潔'ed contitions。

2

這是做這件事:

if (returnVal == 'GIN') or (returnVal == 'NOT READY') or returnVal == '...': 

雖然更Python更好的辦法是使用in

if returnVal in ['GIN', 'NOT READY', '...']: 

換句話說(對於第一種情況),使用單獨條件,只是or他們在一起。

你總是看到INSIDE的原因是因爲'GIN'是在有條件的情況下有效的一個被視爲true值:

>>> if 'GIN': 
...  print "yes" 
... 
yes 

true or <anything>true

6

您的代碼,邏輯,內容是這樣的:

if 'GIN' exists 
or if 'NOT READY' exists 
or if 'TO BE ABANDON OR NEEDS RESUBMISSION' is equal to retVal 
    do something 

閱讀本link關於蟒蛇真值(這也與paxdiablo的答案)。

更好的辦法是使用Python 「中的」 聲明:

if retVal in ['GIN', 'NOT READY', 'TO BE ABANDON OR NEEDS RESUBMISSION']: 
    do something