简体   繁体   中英

python - Pass/Sending return variables from one function to another

I was trying to pass two variables returned from one function to the next function but but I'm not really understanding what am I missing, i always get the error -

TypeError: Function2() takes exactly 2 arguments (0 given)

My Code:

Function1(arg1, arg2):
# This two args to this function are taken from the user and some work is done here and this function will return two things as output (my_list1 is a list) - 
return my_list1, count

Function2 (argg1, argg2):
# This function will get the first and second argument from the previous function (Function1)

def main():
                Function1(list1, count1)
                myfinal_list, final_count = Function1()
                Function2(myfinal_list, final_count)

if __name__== "__main__":
        main()

How can I achieve this? What would I have to do to make sure data from the first function will be sent to the second function? Thank you!

Try this:

def main():        
    myfinal_list, final_count = Function1(list1, count1)
    Function2(myfinal_list, final_count)

Since this sentence myfinal_list, final_count = Function1() will give you an error because you are calling a Funcion1 with no arguments (while 2 are expected).

You're very close, but just missing one part:

In short, you have to include the arguments when calling the function. For instance, in the line: myfinal_list, final_count = Function1() you don't call either argument.

Accordingly, main() should be rewritten as follows:

def main():
    myfinal_list, final_count = Function1(list1, count1)
    Function2(myfinal_list, final_count)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM