<p>我试图创建一个函数来检查字符串是否包含单词,但我一直运行到这个错误<code>'in <string>' requires string as left operand, not tuple</code>我对python相当陌生,所以我不确定它为什么不工作。感谢您的帮助。提前谢谢</p>
<pre><code>def syntaxCheck(s):
words = ("for", "if", "else", "elif", "while", "def", "not")
if words in s:
print("true")
else:
print("false")
syntaxCheck("if while else")
</code></pre>
<p>您可以尝试使用<code>s_list = s.split()</code>将字符串转换为单词列表,并在迭代列表并比较每个值后,如下所示:</p>
<pre class="lang-python prettyprint-override"><code>flag = False
for i in s_list:
if i in words:
flag = True
</code></pre>