Home > Blockchain >  Missing Positional Arguments Python (Directing to a function)
Missing Positional Arguments Python (Directing to a function)

Time:11-10

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()

CodePudding user response:

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!

CodePudding user response:

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.

CodePudding user response:

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()
  • Related