How to assign particular return variable in function on if/else statement in python
Question:
I have two functions as below:
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()
Current Output:
something else:
Expected Output:
Not Done
I want to know how to check particular return
variable for particular if/else statement.
Answers:
Simply this?
def xyz():
i, j = abc()
if i == "False":
print("Not Done")
elif j == "101":
print("something else:")
If you want your code to work, since your function is returning a tuple:
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:")
I’m guessing that you want to print not done if any variable inside your function abc() is "False". In that case here’s the answer:
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()
I have two functions as below:
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()
Current Output:
something else:
Expected Output:
Not Done
I want to know how to check particular return
variable for particular if/else statement.
Simply this?
def xyz():
i, j = abc()
if i == "False":
print("Not Done")
elif j == "101":
print("something else:")
If you want your code to work, since your function is returning a tuple:
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:")
I’m guessing that you want to print not done if any variable inside your function abc() is "False". In that case here’s the answer:
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()