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.

Asked By: Divyank

||

Answers:

Simply this?

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

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:")
Answered By: Talha Tayyab

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()
Answered By: Nissan Apollo
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.