同时突破多项功能

时间:2018-08-09 14:11:44

标签: python-3.x

我使用了搜索功能,但是找不到我的问题的明确答案。这出现了,但并没有真正回答:Breaking out of a recursive function?

我只是试图创建一种退出按钮,这就是程序总体布局的方式,我将保留所有细节。

def function_1():
while True:
    print('Things...')
    input1 = input('type a, b or c')
    if input = 'a':
        function_2()

def function_2():
while True:
    print('More fun things...')
    input2 = input('type 1, 2, "x" to return to previous menu or "exit" to exit')
    if input2 = 'exit':
        ?????

这听起来像是一个简单的问题,但是我不确定该怎么做,尝试了一些没有成功的事情。 为了明确起见,“退出”应同时退出这两个功能。

谢谢!

1 个答案:

答案 0 :(得分:2)

function_2应该return一个值,而function_1应该捕获该返回值并相应地表现。例如:

def function_1():
    while True:
        print('Things...')
        input1 = input('type a, b or c')
        if input = 'a':
            quit = function_2()
            if quit:
                return

def function_2():
    while True:
        print('More fun things...')
        input2 = input('type 1, 2, "x" to return to previous menu or "exit" to exit')
        if input2 = 'exit':
            return True