SuperUser_Novice SuperUser_Novice - 1 year ago 73
Python Question

Python3 Skipping Flow Check

I'm having a bit of trouble with my python3 persistent password locker. It stores and retrieves passwords as expected/desired, but I decided to insert a "backout" check so that if information is entered incorrectly you can go back and re-enter it. It seems to be skipping this check for no easily identifiable reason.

elif choice == 2:
conti = False
while conti != True:
print('Enter an account and password to store')
acc = input('Account: ')
apass = input('Password: ')
print('Account: ' + acc + '\n' + 'Password: ' + apass)
corr = input(': ')
if corr == 'yes' or 'ye' or 'y':
print('Making Changes persistent')
# Shelve Stuff
conti = True
print('Please make appropriate changes.')

When I run this code it makes changes persistent regardless of what the corr variable happens to be. This is not what I want. I tried explicitly stating in an elif statement the no options and it skipped those as well. Are the multiple 'or' statements throwing it off or is there something else going on that I should be aware of?

Answer Source

This line is not doing what you'd hoped:

 if corr == 'yes' or 'y' or 'y':

This translates to "is corr equal to 'yes'? It's not! So, is the value 'y' True? It is! Let's do this!" Note, it's not checking that corr is equal to 'y', just that the string 'y' is Truthy.

The longhand way would be:

if corr == 'yes' or corr == 'y':

but you could also do:

if corr in ['yes', 'y', 'Y', 'YES']:

or something similar to cover more options

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download