Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python语法:如果C中的A和B执行某些操作_Python_Syntax - Fatal编程技术网

Python语法:如果C中的A和B执行某些操作

Python语法:如果C中的A和B执行某些操作,python,syntax,Python,Syntax,我试图将以下代码作为更大脚本的一部分来编写(我认为脚本的其余部分与这个特定问题无关,但请告诉我是否相关!): 我遇到的问题是,字符串“Ok”不是在两个条件都满足时打印的(如果C中的A和B),而是在其中只有一个条件发生时打印的,也就是说,即使C中只有A,但B不满足,也会打印“Ok” 你知道我可能遗漏了什么吗 谢谢 你需要这样写: if A in C and B in C: # do your thing if (A) and (B in C): # your thing 您的代码

我试图将以下代码作为更大脚本的一部分来编写(我认为脚本的其余部分与这个特定问题无关,但请告诉我是否相关!):

我遇到的问题是,字符串“Ok”不是在两个条件都满足时打印的(
如果C中的A和B
),而是在其中只有一个条件发生时打印的,也就是说,即使C中只有A,但B不满足,也会打印“Ok”

你知道我可能遗漏了什么吗


谢谢

你需要这样写:

if A in C and B in C:
   # do your thing
if (A) and (B in C):
    # your thing
您的代码所发生的事情是,尽管它在简单的英语中听起来很神奇(“如果A和B在C中”),但它实际上是这样计算的:

if A in C and B in C:
   # do your thing
if (A) and (B in C):
    # your thing
因此,当
A
对象
不是None
时,
and
子句的第一部分将计算为
True
,结果将取决于
B
在C中是否为

因此,您需要在
子句的两侧分别使用
in
运算符,正如我建议的那样:

if (A in C) and (B in C):
    # your thing (parenthesis not needed, but explicitly put there for clarity

你需要这样写:

if A in C and B in C:
   # do your thing
if (A) and (B in C):
    # your thing
您的代码所发生的事情是,尽管它在简单的英语中听起来很神奇(“如果A和B在C中”),但它实际上是这样计算的:

if A in C and B in C:
   # do your thing
if (A) and (B in C):
    # your thing
因此,当
A
对象
不是None
时,
and
子句的第一部分将计算为
True
,结果将取决于
B
在C
中是否为

因此,您需要在
子句的两侧分别使用
in
运算符,正如我建议的那样:

if (A in C) and (B in C):
    # your thing (parenthesis not needed, but explicitly put there for clarity
您可以使用
all()

您可以使用
all()


这是
如果A和(B在C中):
如果A和(B在C中):
如果A和(B在C中):如果A和(B在C中):如果A和B在C中不做你认为的事。。。