简体   繁体   English

如果'z'中的'x'和'y':

[英]if 'x' and 'y' in 'z':

I'm making sort of a Q&A script in python. 我正在python中编写一个Q&A脚本。 It gets raw_input, and sets it as theQuestion. 它获取raw_input,并将其设置为问题。 I tried if 'var1' and 'var2' in theQuestion: , but it looks for either string, not both. 我在问题中尝试了if 'var1' and 'var2' in theQuestion:但是它找到了两个字符串,而不是两个字符串。 Is there a way I can make this work in one 'if' statement? 有没有办法让我在一个'if'语句中完成这项工作? (not 'if x: if y: then z). (不是'if x:if y:then z)。

and is a logical AND, not a natural-language one. and是一个逻辑AND,而不是自然语言。 Therefore, your code gets interpreted as: 因此,您的代码被解释为:

'var1' and 'var2' in theQuestion
True   and 'var2' in theQuestion # Since bool('var1') == True
           'var2' in theQuestion

You want to connect the two tests with a logical AND: 您希望使用逻辑AND连接两个测试:

if 'var1' in theQuestion and 'var2' in theQuestion:

Alternatively, for large numbers of tests: 或者,对于大量测试:

if all(k in theQuestion for k in ('var1', 'var2')):

How about: 怎么样:

if 'x' in z and 'y' in z:
  ... do something ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM