Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python "if" condition difference between "and" & "or"

I want the user to decide whether or not a selected file gets deleted by saying exactly "yes" or "no". For what ever reason "yes" works however "no" loops back round and decides to go back to initial if statement

I have tried a few things, after some google searches I cannot seem to find a response that is suited to this issues. I have tried:

Changing the inputText with str() and without str()

I have tried Quotes and apostrophes on string texts.

I have tried changing the initial If Statement:

First:

if inputText != ('yes' or 'no'):

and I have even tried

Second:

if inputText != 'yes' or inputText != 'no':

Third:

if (inputText != 'yes') or (inputText != 'no'):

The closest I've got is First. For some reason 'yes' seems to work but 'no' seems to loop back to the first If Statement.

Can anyone point me in the right direction? What am I missing?

inputText = str(input("Are you sure you want to delete file? (yes/no) "))
ctrl = True
while ctrl == True:
    if inputText != ('yes' or 'no'):
        inputText = input('Please type "yes" or "no" ')
    elif inputText == 'yes':
        ##delete
        print("pretend got deleted")
        ctrl = False
    elif inputText == 'no':
        ##shit does not get deleted
        print("pretend doesn't got deleted")
        ctrl = False

The Results I anticipated would be a simple

>> Are you sure you want to delete file? (yes/no) *random text that isn't 'yes' or 'no'*
>> Please type "yes" or "no"

This is what I get with First of my initial If Statement example:

>> Are you sure you want to delete file? (yes/no) yes
>> pretend got deleted

This is what I anticipate:

>> Are you sure you want to delete file (yes/no) no
>> pretend doesn't got deleted

This is what I actually get

>> Are you sure you want to delete file (yes/no) no
>> Please type "yes" or "no" 

even if I do:

>> Are you sure you want to delete file (yes/no) no
>> Please type "yes" or "no" yes
>> pretend got deleted

is the result I get

like image 908
Lyncius Avatar asked Aug 14 '26 03:08

Lyncius


1 Answers

The issue lies with in your first if statement. Your first if, if inputText != ('yes' or 'no') resolves to true as soon as it sees that your input is not yes. So change your if to resolve to true only when input is not yes and not no which resolves to if inputText != ('yes') and inputText != ('no')

Your code would look as below

inputText = str(input("Are you sure you want to delete file? (yes/no) "))
ctrl = True
while ctrl == True:
    if inputText != ('yes') and inputText != ('no'):
        inputText = input('Please type "yes" or "no" ')
    elif inputText == 'yes':
        ##delete
        print("pretend got deleted")
        ctrl = False
    elif inputText == 'no':
        ##shit does not get deleted
        print("pretend doesn't got deleted")
        ctrl = False
like image 81
Coder Avatar answered Aug 16 '26 19:08

Coder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!