How to continue after "return" - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to continue after "return" (/thread-16815.html) |
How to continue after "return" - Helmi - Mar-16-2019 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! RE: How to continue after "return" - ichabod801 - Mar-16-2019 if you need to return two things, you return a tuple: def add_mul(x, y): return x + y, x * ySo 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. RE: How to continue after "return" - Helmi - Mar-16-2019 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. RE: How to continue after "return" - Yoriz - Mar-16-2019 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() RE: How to continue after "return" - Helmi - Mar-17-2019 Thank you for additional explanation. Now it is clear. |