I am studying python 3. I know I should probably use argparse, but I wanted a simple test of a string in an if statement, getting data from the input() function.
If I test for 2 different strings with == things work as expected But if I reverse my if statement and test with != it does not seem to work
here is the code that works as expected
import sys
import os
tdown = input("topdown true? type t or f: ")
print("tdown", type(tdown), tdown)
if tdown == 't' or tdown == 'f':
pass
else:
print(" need to type 't' or 'f', exiting")
sys.exit(1)
print("passed arg test, tdown is: ", tdown)
running the code, and here are the results
./os_walk.py
topdown true? type t or f: t
tdown <class 'str'> t./os_walk.py
passed arg test, tdown is: t
./os_walk.py
topdown true? type t or f: f
tdown <class 'str'> f
passed arg test, tdown is: f
./os_walk.py
topdown true? type t or f: x
tdown <class 'str'> x
need to type 't' or 'f', exiting
here is the code using != which does not work as expected
import sys
import os
tdown = input("topdown true? type t or f: ")
print("tdown", type(tdown), tdown)
if tdown != 't' or tdown != 'f':
print(" need to type 't' or 'f', exiting")
sys.exit(1)
else:
pass
print("passed arg test, tdown is: ", tdown)
if I run this we get these results
./os_walk.py
topdown true? type t or f: t
tdown <class 'str'> t
need to type 't' or 'f', exiting
./os_walk.py
topdown true? type t or f: x
tdown <class 'str'> x
need to type 't' or 'f', exiting
I dont understand why the != is giving me an unexpected result, can anyone explain please?
Aucun commentaire:
Enregistrer un commentaire