2012-04-27 59 views
0

我在elif語句中選擇Y和N有問題。 如果我輸入n,它會正確輸出n的elif選擇,但如果我輸入y,它仍會輸出n選項而不是elif選擇y。我不知道爲什麼這樣做。對於代碼感到抱歉,如果錯誤清楚,我是python新手。
我檢查了選擇,它確實保持n或y的選擇,但只是執行n,即使輸入y也是如此。elif statment始終執行,即使輸入有誤

if os.path.exists('ExifOutput.txt'): 
        print "The file appears to already exist, would you like to overwrite it?" 
        Choice = raw_input("Y/N : ") 
        if not re.match("^[Y,y,N,n]*$", Choice): 
         print "Error! Only Choice Y or N allowed!" 
        elif len(Choice) > 1: 
         print "Error! Only 1 character allowed!" 
        elif not Choice: 
         print "No choice made" 
        elif Choice == 'N' or 'n': 
         print "Save the old file to a different directory and try again" 
         ExifTags() 
        elif Choice == 'Y' or 'y': 
         print "The file will be overwritten by a new one" 
         ExifRetrieval(listing, FileLocation) 
         print "Completed" + "\n" 
       else: 
        ExifRetrieval(listing, FileLocation) 
        print "Completed" + "\n" 
+0

注意,[PEP-8](http://www.python.org/dev/peps/pep-0008/)建議保留'CapWords'對類名,和''lowercase_with_underscores'用於函數名和局部變量。 – 2012-04-27 14:06:16

+0

僅供參考:如果您立即將您的輸入轉換爲小寫(或大寫),那麼您將不必擔心比較多種情況,而且您的邏輯將更簡單。 – Levon 2012-04-27 14:26:00

回答

4

Choice == 'N' or 'n'始終是真實的(這是一樣的(Choice == 'N') or 'n')。你想要Choice in ('N', 'n')

+0

啊,是的,就是這樣。感覺一個傻瓜沒有看到它,它現在起作用。非常感謝。 – Owny198 2012-04-27 14:01:37

0
elif Choice == 'N' or Choice == 'n': 

elif Choice in ("N","n"): 
相關問題