mardi 14 janvier 2020

if+or fails even though inverse condition works

I'm teaching myself python and I am trying to understand why the below example fails.

#!/usr/bin/python3.4
vWHEN = sys.argv[1]
print (vWHEN)
if vWHEN != "before" or vWHEN != "after":
 exit("ERROR: " + vWHEN + " is invalid. Should be: 'before' or 'after'")

The output is as follows:

$ ptest.py before
before
ERROR: before is invalid. Should be: 'before' or 'after'

But the following does work:

#!/usr/bin/python3.4
vWHEN = sys.argv[1]
if vWHEN == "before" or vWHEN == "after":
 print (vWHEN)
else:
 exit("ERROR: " + vWHEN + " is invalid. Should be: 'before' or 'after'")

Output:

$ ptest.py before
before

Aucun commentaire:

Enregistrer un commentaire