是否可以从Python中的另一个函数停止循环?

时间:2019-02-04 22:17:51

标签: python function loops

我想要一个从主菜单开始的程序,该菜单会分支到不同的功能。在每个功能的结尾,我希望询问用户“是否要返回到主菜单?”。如果用户说不,我希望程序通过停止主循环来终止(我不想使用sys.exit()或类似的东西)。 示例代码:

#Issue is almost at the bottom
#Feel free to comment on the rest of the code as well,
#Always looking to improve
def main():
    loop = True
    while loop:
        print('''MENU CHOICE''')
        print('''1: go here''')
        print('''2: go there''')
        print('''3: You get the point''')
        print('''0: Terminate program''')
        print()

        try:
            answer = int(input('''I want to go to program: '''))
        except:
            print('''Not a valid menu choice, please try again''')
            print()

        if answer != 1 and answer != 2 and answer != 3 and answer != 0:
            print('''Not a valid menu choice, please try again''')
            print()
        elif answer == 1:
            program1()
        elif answer == 2:
            program2()
        elif answer == 3:
            program3()
        else:
            loop = False

def program1():
    print('''This is program 1''')
    itdontwork = input('''Do you want to go back to the menu? Y/N''')

    if itdontwork == 'Y' or itdontwork == 'y':
        print()
    else:
        print('''SHUTTING DOWN''')
        loop = False #Here is the issue

#The rest of the programs would be the same

main()

5 个答案:

答案 0 :(得分:2)

问题是您试图更改在program1函数范围之外定义的变量。 loop是在main内部定义的,因此只有main可以访问它。有几种方法可以解决此问题,您可以在外部声明loop(使其成为全局变量),也可以只使program1向调用者函数返回布尔值,例如:

def main():
    loop = True
    while loop:
        loop = program1()

def program1():
    itdontwork = input('''Do you want to go back to the menu? Y/N''')
    if itdontwork == 'Y' or itdontwork == 'y':
        print()
    else:
        print('''SHUTTING DOWN''')
        return False

答案 1 :(得分:1)

我认为您想尝试做的是让不同的程序返回一个值。

问题是函数被执行但什么都不返回。您的主函数不返回任何内容。因此,循环变量永远无法更新以打破循环。

#Issue is almost at the bottom
#Feel free to comment on the rest of the code as well,
#Always looking to improve
def main():
    loop = True
    while loop:
        print('''MENU CHOICE''')
        print('''1: go here''')
        print('''2: go there''')
        print('''3: You get the point''')
        print('''0: Terminate program''')
        print()

        try:
            answer = int(input('''I want to go to program: '''))
        except:
            print('''Not a valid menu choice, please try again''')
            print()

        if answer != 1 and answer != 2 and answer != 3 and answer != 0:
            print('''Not a valid menu choice, please try again''')
            print()
        elif answer == 1:
            return program1() # Return the output of this function
        elif answer == 2:
            return program2() # Return the output of this function
        elif answer == 3:
            return program3() # Return the output of this function
        else:
            loop = False

def program1():
    print('''This is program 1''')
    itdontwork = input('''Do you want to go back to the menu? Y/N''')

    if itdontwork == 'Y' or itdontwork == 'y':
        print()
    else:
        print('''SHUTTING DOWN''')
        return False # Return the output of this function

#The rest of the programs would be the same

main()

答案 2 :(得分:0)

最简单的方法是使变量循环全局化

loop = True
def main():
    global loop
    while loop:
        print('''MENU CHOICE''')
        print('''1: go here''')
        print('''2: go there''')
        print('''3: You get the point''')
        print('''0: Terminate program''')
        print()

        try:
            answer = int(input('''I want to go to program: '''))
        except:
            print('''Not a valid menu choice, please try again''')
            print()

        if answer != 1 and answer != 2 and answer != 3 and answer != 0:
            print('''Not a valid menu choice, please try again''')
            print()
        elif answer == 1:
            program1()
        elif answer == 2:
            program2()
        elif answer == 3:
            program3()
        else:
            loop = False

def program1():
    global loop
    print('''This is program 1''')
    itdontwork = input('''Do you want to go back to the menu? Y/N''')

    if itdontwork == 'Y' or itdontwork == 'y':
        print()
    else:
        print('''SHUTTING DOWN''')
        loop = False #Here is the issue

#The rest of the programs would be the same

main()

Global将使您到处都有相同的w / r变量。如果没有全局所有变量,则为局部变量。

答案 3 :(得分:-1)

...
        elif answer == 1:
            loop = program1()
        elif answer == 2:
            loop = program2()
        elif answer == 3:
            loop = program3()
        else:
            loop = False

def program1():
    print('''This is program 1''')
    itdontwork = input('''Do you want to go back to the menu? Y/N''')

    if itdontwork == 'Y' or itdontwork == 'y':
        print()
        return 1 # back to menu...
    else:
        print('''SHUTTING DOWN''')
        return 0

这将根据需要从被调用的函数到循环中获取所需的数据。

答案 4 :(得分:-2)

您可以raise ValueError('Words and stuff'),然后将其困住。

if answer != 1 and answer != 2 and answer != 3 and answer != 0:
    print('''Not a valid menu choice, please try again''')
    print()
elif answer == 1:
    try:
        program1()
    except ValueError:
        break
elif answer == 2:
    try:
        program2()
    except ValueError:
        break
elif answer == 3:
    try:
        program3()
    except ValueError:
        break
else:
    loop = False


def program1():
    print('''This is program 1''')
    itdontwork = input('''Do you want to go back to the menu? Y/N''')

    if itdontwork == 'Y' or itdontwork == 'y':
        print()
    else:
        print('''SHUTTING DOWN''')
        # loop = False #Here is the issue
        raise ValueError('BOOM SHAKA LAKA!')