Python Forum

Full Version: How to continue after "return"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I need help with combining two if statements. The problem is that both need to return something but the function goes out of scope after the first "return" and does not continue.

def comparison(industry, salary):
    if industry == "production":
        return "Average salary of this industry was " + str(df['Average'].min()) + "."
        if salary > int(df['Average'].min()):
            return "Above the average salary"
        elif salary < int(df['Average'].min()):
            return "Below the average salary"
        elif salary == int(df['Average'].min()):
            return "Average salary"
                 
    else:
        return "Yearly average salary was " + str(df['Average'].max()) + "."
        if salary > int(df['Average'].max()):
            return "Above the average salary"
        elif salary < int(df['Average'].max()):
            return "Below the average salary"
        elif salary == int(df['Average'].max()):
            return "Average salary" 
I believe there is some efficient way to combine these two IF statements but unfortunately I have not been able to figure it out although I have "googled".
Thank you!
if you need to return two things, you return a tuple:

def add_mul(x, y):
    return x + y, x * y
So on lines 3 and 12 I would store what your are returning there in a variable, and return that variable along with the other values returned later.
Thank you for your reply. Please advise how I could use a variable instead of return. Unfortunately I do not know how to do it.
def some_function():
    if something:
        return1 = '1st thing'
        if somthing_else:
            return2 = '2nd thing'
        elif another_thing:
            return2 = '3rd thing'
    
    else:
        return1 = '4th thing'
        if somthing_else:
            return2 = '5th thing'
        elif another_thing:
            return2 = '6th thing'
    
    return return1, return2

        
return1, return2 = some_function()
Thank you for additional explanation. Now it is clear.