0

Is there a way to return user_input_1 and user_input_2 into the function_3 without using temp_var_1 and temp_var_2 and insert it directly?

What is the correct way of doing it?

def function_1():
    user_input_1 = input("\nInput the first word: ")
    return user_input_1

def function_2():
    user_input_2 = input("\nInput the second word: ")
    return user_input_2

def function_3(user_input_1,user_input_2):
    user_input_total = user_input_1 + user_input_2
    print(user_input_total)

def main():
    temp_var_1 = function_1()
    temp_var_2 = function_2()
    function_3(variable_1,variable_2)

main()

This is not working.

def function_1():
    user_input_1 = input("\nInput the first word: ")
    return user_input_1

def function_2():
    user_input_2 = input("\nInput the second word: ")
    return user_input_2

def function_3(user_input_1,user_input_2):
    user_input_total = user_input_1 + user_input_2
    print(user_input_total)

def main():
    function_1()
    function_2()
    function_3(function_1,function_2)

main()
0

2 Answers 2

1

You can do something like this:

In [885]: def function_3(): 
     ...:     print(function_1() + function_2()) 
     ...:   
  
In [883]: def main(): 
     ...:     function_3() 
     ...:                                                                                                                                                                                                   

In [884]: main()                                                                                                                                                                                            

Input the first word: Stack

Input the second word: Overflow
StackOverflow

Or by your method, you need to change your main() function to below:

def function_3(user_input_1,user_input_2):
    user_input_total = user_input_1 + user_input_2
    print(user_input_total)

def main():
    function_3(function_1(),function_2())

main()
Sign up to request clarification or add additional context in comments.

2 Comments

What would be best practice to use? If you had to review code for someone or had to work with someone, which version would you prefer that person is using?
It's always best to break code into reusable functions. I would use the way that you used in your question, using temp_variables to store functions values and then returning the output.
1

Note: There are various ways to do that:

I hope this resolves your problem:

def function_1():
    user_input_1 = input("\nInput the first word: ")
    return user_input_1

def function_2():
    user_input_2 = input("\nInput the second word: ")
    return user_input_2

# Call function 1 & 2 from function 3 itself.
def function_3():
    print(function_1() + function_2())

# Call Main Func
def main():
    function_3()

main()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.