python - 单词中的多个字符串

标签 python python-2.7

<分区>

我正在尝试这样做 -

word = 'hello'

if 'a' or 'c' in word:
    continue
elif 'e' in word:
    continue
else:
    continue

它只有在我没有 or 时才有效。该字符串是一个单词。

最佳答案

这个:

if 'a' or 'c' in word:
    continue

相当于:

if ('a') or ('c' in word):
    continue

并且 'a' 在 bool 上下文中始终为 True,因此 continue 总是由于短路而执行。

解决方法:

if any(c in word for c in ('a', 'c')):
    continue

关于python - 单词中的多个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29828339/

相关文章:

python - 扩展不可变(卡住)数据类

python - 如何在for循环中移动多只 turtle ?

PHP 和 python 节俭

python - Windows 上的 Python 模块

python-2.7 - 为什么我在尝试升级 pip 后收到 DistributionNotFound 错误?

带掩码的 Python OpenCV matchTemplate - 在所有位置找到匹配项

python - 400 错误 : Invalid URI when uploading new PyPI package (twine)

python - 如何解决索引错误: list assignment index out of range error?

python - 为 Python 3 安装 pip

python - 在 Windows 中使用 crypt 模块?