2012-04-27 4 views
0

私はelifステートメントのYとNの選択に問題があります。 私がnを入力すると、elifのnの選択が正しく出力されますが、yを入力すると、elifの選択肢yではなくnの選択肢が出力されます。なぜこれをやっているのか分かりません。コードを残念に思ってしまい、間違いが晴れていれば、私はPythonの初心者です。
私は選択肢をチェックして、nまたはyの選択を維持しますが、yが入力されてもnを実行します。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''があります。 –

+0

ちょうどFYI:すぐに入力を小文字(または大文字)に変換すると、複数のケースを比較する心配がなくなり、ロジックが簡単になります。 – Levon

答えて

4

Choice == 'N' or 'n'(それは(Choice == 'N') or 'n'と同じです)常に真です。あなたはChoice in ('N', 'n')が欲しい。

+0

ああそうです。それを見ないために愚か者を感じ、それは今働く。どうもありがとうございます。 – Owny198

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

又は

elif Choice in ("N","n"): 
関連する問題