Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python字符串如果x!=y没有按预期工作_Python_String_If Statement_Testing - Fatal编程技术网

python字符串如果x!=y没有按预期工作

python字符串如果x!=y没有按预期工作,python,string,if-statement,testing,Python,String,If Statement,Testing,我正在学习python 3。 我知道我可能应该使用argparse,但我希望对if语句中的字符串进行简单测试,从input()函数获取数据 如果我用==测试两个不同的字符串,事情会按预期工作 但是如果我反转我的if语句并使用!=它似乎不起作用 下面是按预期工作的代码 import sys import os tdown = input("topdown true? type t or f: ") print("tdown", type(tdown), tdown) if tdown ==

我正在学习python 3。 我知道我可能应该使用argparse,但我希望对if语句中的字符串进行简单测试,从input()函数获取数据

如果我用==测试两个不同的字符串,事情会按预期工作 但是如果我反转我的if语句并使用!=它似乎不起作用

下面是按预期工作的代码

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)
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)
运行代码,下面是结果

./os_walk.py 如果我运行这个,我们会得到这些结果

./os_walk.py
自上而下为真?类型t或f:t
土崩瓦解
需要键入“t”或“f”,正在退出
./os_walk.py
自上而下为真?类型t或f:x
时差x
需要键入“t”或“f”,正在退出
我不明白为什么给了我一个意想不到的结果,有人能解释一下吗?


哇,太快了-谢谢,我明白你的意思了,“和”需要用来测试2张底片。“或”仅适用于阳性试验。真不敢相信我居然没看到。哇,那太快了-谢谢,我明白你的意思了,“和”需要用来测试两张底片。“或”仅适用于阳性试验。真不敢相信我没看到。
topdown true? type t or f: f
tdown <class 'str'> f
passed arg test, tdown is:  f
topdown true? type t or f: x
tdown <class 'str'> x
 need to type 't' or 'f', exiting
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)
topdown true? type t or f: t
tdown <class 'str'> t
 need to type 't' or 'f', exiting
topdown true? type t or f: x
tdown <class 'str'> x
 need to type 't' or 'f', exiting
if  tdown != 't' and tdown != 'f':
   ...