Owny198
Owny198

Reputation: 5

elif statment always executing even with wrong input

I Have a problem with the Y and N choice in the elif statements. If I enter n it correctly outputs the elif choice of n, but if I input y it still outputs the n choice instead of the elif choice y. I have no idea why it's doing this. Sorry about the code and if the mistakes clear as day, I'm new to python.
I have checked choice and it does keep the choice of n or y but just executes n even if y is input.

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"

Upvotes: 0

Views: 159

Answers (2)

HYRY
HYRY

Reputation: 97331

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

or

elif Choice in ("N","n"): 

Upvotes: 0

Cat Plus Plus
Cat Plus Plus

Reputation: 129954

Choice == 'N' or 'n' is always true (it's the same as (Choice == 'N') or 'n'). You want Choice in ('N', 'n').

Upvotes: 4

Related Questions