我想这样做 -
word = 'hello'
if 'a' or 'c' in word:
continue
elif 'e' in word:
continue
else:
continue
Run Code Online (Sandbox Code Playgroud)
只有在我没有或者它时它才有用.字符串是一个单词.
这个:
if 'a' or 'c' in word:
continue
Run Code Online (Sandbox Code Playgroud)
相当于:
if ('a') or ('c' in word):
continue
Run Code Online (Sandbox Code Playgroud)
并且'a'总是True在布尔上下文中,因此continue总是由于短路而执行.
解:
if any(c in word for c in ('a', 'c')):
continue
Run Code Online (Sandbox Code Playgroud)