<分区>
我正在尝试这样做 -
word = 'hello'
if 'a' or 'c' in word:
continue
elif 'e' in word:
continue
else:
continue
它只有在我没有 or 时才有效。该字符串是一个单词。
标签 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/