python - 如何在 python 中创建 OR 语句?

标签 python

为什么这不起作用:

            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/

相关文章:

python - 随机选择一个函数

python - 如何在不同的类中使用 Slot+Signal?

python - Django 基于类的 View : How do I pass additional parameters to the as_view method?

Python 请求 ssl [Errno 1] _ssl.c :1428: error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number

python - 如何在python中对列表进行排序后获取原始索引

python - 在 Jupyter Notebook 中运行 Tornado 服务器

python - Python 中的动态对象类似于 AS3

python asyncio.Event.wait() 没有响应 event.set()

python - 是否有一个 Python 函数可以一次性计算出一个单词中的所有项目?

python - 如果列表中的元素在另一个列表中,则删除该列表的元素