1

I'm trying to figure out how to direct a function to a function. What I'm trying to do is answer a prompt question y/n that would run a certain function. When I input y, it will run through both functions instead of only function 1.

Thanks!

def company_type(question, public_company, private_company):
    print("Is the target company public on NYSE or NASDAQ?")
    prompt = f'{question} (y/n)'
    ans = input(prompt)
    if ans == 'y':
        return (public_company)
    if ans == 'n':
        print("Please enter financial infomation manually.")
        return (private_company)
company_type("public_company", "private_company", 1)

# function 1 
def public_company(): 
    return (print("Success 1"))
public_company()

# function 2
def private_company():
   return (print("Success 2"))
private_company()

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • What do you expect `function(public_company)` to do? You haven't defined `function` and it's not built in to Python. Why not just call `public_company` or `private_company`? – kindall Nov 09 '21 at 05:53
  • Does this answer your question? [Creating a new function as return in python function?](https://stackoverflow.com/questions/12738031/creating-a-new-function-as-return-in-python-function) – AJ Biffl Nov 09 '21 at 05:54
  • Or this? [How do I write a function that returns another function?](https://stackoverflow.com/questions/14261474/how-do-i-write-a-function-that-returns-another-function/14271106) – AJ Biffl Nov 09 '21 at 05:55

3 Answers3

1

You don't really want to return a function. You just want one function to call another function. That's done like this:

# function 1 
def public_company(): 
    return print("Success 1")

# function 2
def private_company():
    return print("Success 2")

def company_type(question, public_company, private_company):
    print("Is the target company public on NYSE or NASDAQ?")
    prompt = f'{question} (y/n)'
    ans = input(prompt)
    if ans == 'y':
        return public_company()
    else:
        print("Please enter financial information manually.")
        return private_company()

company_type("some question", public_company, private_company)

And please note that return statements in Python do not use an extra set of parentheses. That's a C idiom.

AJ Biffl
  • 493
  • 3
  • 10
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
1

You can absolutely return a function to be used later - this is the essence of functional programming, and the reason to have functions as first class objects in python ~ Guido Van Rossum.

The important thing is to remember that parens mean a function call, whereas no parens mean the function object.

def public_company():
    print("we do what public companies do")

def private_company():
   print("we do what private companies do")


def choose_company():
    ans = input("Is the target company public?")
    if ans == 'y':
        return public_company   # no parens
    else:
        return private_company  # no parens


if __name__ == '__main__':
    # assign the returned function to selected_company
    selected_company = choose_company()

    # calling selected_company() will call the selected function
    selected_company()   # use parens, this is a function call!
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
0

Errors:-

  • function is not a keyword.
  • We can call a function by:- <function_name>().
# function 1 
def public_company(): 
    print("Success")
    
# function 2
def private_company():
    print("Success")

def company_type():
    print("Is the target company public on NYSE or NASDAQ?")
    prompt = 'Is your company a public company? (y/n)'
    ans = input(prompt)
    if ans == 'y':
        public_company()
    if ans == 'n':
        print("Please enter financial information manually.")
        private_company()
company_type()
Bibhav
  • 1,579
  • 1
  • 5
  • 18