0

elif ステートメントの Y と N の選択に問題があります。n を入力すると、n の elif 選択が正しく出力されますが、y を入力すると、elif 選択 y ではなく n 選択が出力されます。なぜこれを行っているのかわかりません。コードについて申し訳ありません。間違いが明らかになった場合、私はPythonが初めてです。
選択を確認しましたが、n または y の選択は保持されますが、y が入力されていても n が実行されます。

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"
4

2 に答える 2

4

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

于 2012-04-27T13:55:38.143 に答える
0
elif Choice == 'N' or Choice == 'n':

また

elif Choice in ("N","n"): 
于 2012-04-27T13:56:36.350 に答える