Возврат между функциями в Python - PullRequest
0 голосов
/ 01 мая 2020

Есть ли способ вернуть user_input_1 и user_input_2 в function_3 без использования temp_var_1 и temp_var_2 и вставить его напрямую?

Как правильно это сделать?

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

Это не работает.

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

Ответы [ 2 ]

1 голос
/ 01 мая 2020

Примечание. Существуют различные способы сделать это:

Я надеюсь, что это решит вашу проблему:

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()
1 голос
/ 01 мая 2020

IIU C, Вы можете сделать что-то вроде этого:

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

ИЛИ по вашему методу:

Вам необходимо изменить функцию main() ниже:

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()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...