怎么做"回传" (不知道具体的术语)

时间:2017-10-11 19:30:50

标签: python python-3.x python-3.6

这是我的代码:

def function_one():
    print ("Hello!")
    choice = int(input("Would you like to see the list and the testvar? 1 = yes, 2 = no"))
    if choice == 1:
        #prints the list and testvar
    function_two()

def function_two():
    list = [1, 4, 7, 9]
    testvar = "Yes"
    function_one()

function_one()

如果我之前已经运行过test_one并在以后运行它,我如何在function_one中打印testvar和list?

1 个答案:

答案 0 :(得分:2)

您描述的行为类型称为相互递归,甚至在Python中也不推荐使用它。一般来说,Python中最好避免递归(除非避免递归的缺陷大于使用它的陷阱。"愚蠢的一致性是小思想的大人物和所有这些)。

您应该在一个连续的循环中运行function_one的正文,然后在适用时调用function_two

def function_one():
    while True:  # infinite loop
        print ("Hello!")
        choice = int(input("Would you like to see the list and the testvar? 1 = yes, 2 = no"))
        if choice == 1:
            # I'm guessing at your intended functionality here
            lst, testvar = function_two()
            print(lst, testvar)

def function_two():
    return ([1, 4, 7, 9], "Yes")