如何在python的if/else语句上赋值函数中的特定返回变量



我有以下两个函数:

def abc():
i = "False"
j = "100"
return i,j
def xyz():
if abc() == "False":  #I want to compare "False" with variable "i"
print("Not Done")
else:
abc() == "101"    ##I want to compare "False" with variable "j"
print("something else:")
xyz()

当前输出:

something else:

预期输出:

Not Done

我想知道如何为特定的if/else语句检查特定的return变量。

就这么简单?

def xyz():
i, j = abc()
if i == "False":
print("Not Done")
elif j == "101":
print("something else:")

如果你想让你的代码工作,因为你的函数返回一个元组:

def abc():
i = "False"
j = "100"
return i,j
def xyz():
if abc()[0] == "False":  #I want to compare "False" with variable "i" #[0] for i
print("Not Done")
else:
abc()[1] == "101"    ##I want to compare "False" with variable "j" #[1] for j
print("something else:")

我猜你想打印未完成,如果你的函数abc()中的任何变量是"False"。在这种情况下,答案是:

def abc():
i = "False"
j = "100"
return i,j
m,n=abc()
def xyz():
if m or n == "False":  #I want to compare "False" with variable "i"
print("Not Done")
else:
##I want to compare "False" with variable "j"
print("something else:")
xyz()

最新更新