是否有可能阻止在Python另一个函数的循环?

问题描述 投票:2回答:5

我想,随着主菜单启动一个程序,关闭功能不同的分支。在每个函数结束时,我希望用户要问'你想回到主菜单?如果用户说没有我想要的程序通过停止主循环终止(我不希望使用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()
python function loops
5个回答
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()

全球将允许你有相同的W / R变数无处不在。如果没有全球的所有变量本地。


2
投票

问题是,你正在试图更改在program1功能范围之外定义的变量。 loop被内部main定义因此仅main可以访问它。有几种方法来解决这个问题,你可以声明loop外(使其成为一个全球性),或只是让你的program1返回一个boolean值以调用函数,例如:

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
投票

我想你想尝试做的是有不同的程序返回一个值。

问题是,该功能得到执行,但返回什么。你的主要功能没有返回值;因此,循环变量不能进行更新,以打破循环。

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

-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

这将让你想要的,你从被调用函数需要在循环中的数据。


-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!')
© www.soinside.com 2019 - 2024. All rights reserved.