为什么这不起作用:
file = "v3a2"
if "v1" or "v2" in file:
v1.write(total)
elif "v3" in file:
print "never here?????"
如何制定这个?
最佳答案
if "v1" or "v2" in file:
相当于
if ("v1") or ("v2" in file):
这将永远是 True
因为 bool("v1")==True
你可以说
if any(x in file for x in ["v1", "v2"]):
或
if "v1" in file or "v2" in file:
如果要检查的项目超过 2 或 3 个,带有 any
的版本看起来会更好
关于python - 如何在 python 中创建 OR 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11978463/