Python Forum
How to continue after "return"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to continue after "return"
#1
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!
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply
#4
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()
Reply
#5
Thank you for additional explanation. Now it is clear.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020