Bottom Page

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!
Quote
#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.
micseydel likes this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#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.
Quote
#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()
ichabod801 likes this post
Quote
#5
Thank you for additional explanation. Now it is clear.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Function and return value Roh_80 1 148 Jan-24-2019, 03:15 PM
Last Post: buran
  how to use the return function NLittle17 5 322 Jan-08-2019, 12:01 AM
Last Post: ichabod801
  Script won't continue after command line skip671 7 357 Oct-15-2018, 07:37 PM
Last Post: snippsat
  Def - Return Question tryingtolearnpython 4 512 Jul-04-2018, 03:13 AM
Last Post: tryingtolearnpython
  Trying to figure how to continue my numbering for my "file 1:" chavez814 3 599 Feb-03-2018, 03:34 PM
Last Post: j.crater
  an exception traceback and continue Skaperen 4 714 Jan-17-2018, 08:50 AM
Last Post: Skaperen
  telnet stuck when get Press any key to continue anna 2 1,175 Dec-31-2017, 11:34 AM
Last Post: anna
  if else return gujugolf 3 818 Dec-30-2017, 01:29 PM
Last Post: squenson
  Return Row ID postgresql takaa 1 693 Nov-24-2017, 03:32 AM
Last Post: Larz60+
  Return options Kongurinn 1 749 Sep-28-2017, 03:02 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)