Python 无法正确地跳出循环,也无法确定原因

Python 无法正确地跳出循环,也无法确定原因,python,break,Python,Break,我目前正在从事一个个人python项目。我是新的编码,所以我知道我的代码可能有很多其他错误 在我正在编写的代码中,用户可以选择执行各种操作。按7将允许用户“重置”代码。这只是调用写入所有内容的函数,并将所有变量和列表恢复为默认值。按6键应该允许用户通过中断代码包含在其中的while True循环来“结束”程序 问题是,如果用户已经重置了程序,那么在第一次尝试时结束程序将不起作用。用户必须始终尝试在程序复位后再结束一次程序。我希望结束操作只需要一次尝试,无论程序重置了多少次 下面我只输入了演示这一

我目前正在从事一个个人python项目。我是新的编码,所以我知道我的代码可能有很多其他错误

在我正在编写的代码中,用户可以选择执行各种操作。按7将允许用户“重置”代码。这只是调用写入所有内容的函数,并将所有变量和列表恢复为默认值。按6键应该允许用户通过中断代码包含在其中的while True循环来“结束”程序

问题是,如果用户已经重置了程序,那么在第一次尝试时结束程序将不起作用。用户必须始终尝试在程序复位后再结束一次程序。我希望结束操作只需要一次尝试,无论程序重置了多少次

下面我只输入了演示这一点所需的代码

def robotTaskManager():

    #List of actions, must code in new actions if necessary (if/elif taskNum == index + 1)
    actions = [
        'New Task',
        'View Tasks',
        'Time Update',
        'Robot Update',
        'View Duty Roster',
        'End Program',
        'Reset Program'
        ]


    while True:
        for i in range (len(actions)):
            print(f'{i+1}: {actions[i]}')
        taskNum = int(input('\nWhat action would you like to perform? '))
        print()


        #End Program action, breaks the main while true loop.
        if taskNum == 6:
            endProgram = 'noEndInput'
            print('1: Yes \n2: No')
            while True:
                try:
                    if endProgram == 'noEndInput':
                        endProgram = int(input('\nEnding the program will erase all data.  Are you sure you want to end the program? '))
                    if endProgram == 1:
                        break
                    elif endProgram == 2:
                        print('\n> Ending the program has been canceled.\n')
                    else:  #Rejects all integers that are not 1 or 2
                        endProgram = int(input('\nPlease enter 1 to end the program or 2 to cancel: '))
                        continue
                    break
                except ValueError:  #Rejects all inputs that are not an integer
                    endProgram = 0
            if endProgram == 1:
                break



        #Reset Program action, resets all data to default.
        elif taskNum == 7:
            reset = 'noResetInput'
            print('1: Yes \n2: No')
            while True:
                try:
                    if reset == 'noResetInput':
                        reset = int(input('\nResetting will erase all data.  Are you sure you want to reset the program? '))
                    if reset == 1:
                        print('\n> Program has been reset. \n')
                        robotTaskManager()
                    elif reset == 2:
                        print('\n> Reset has been canceled.\n')
                    else:  #Rejects all integers that are not 1 or 2
                        reset = int(input('\nPlease enter 1 to reset or 2 to cancel: '))
                        continue
                    break
                except ValueError: #Rejects all inputs that are not an integer
                    reset = 0

robotTaskManager()

我已经试着调试了很长一段时间,但一直没有成功。请帮我找出我遗漏了什么。

您的错误是,您没有实际重置程序,而是重复了—您从主函数中调用了主函数。是的,这意味着您可以让您的用户单独中断每个呼叫

您需要重构代码,以便重置可以重新初始化现有程序,而不是启动新程序

这是基本逻辑。您有一个外部循环,只要用户想留在程序中,它就会继续。您有一个处理每个新会话(任务序列)的内部循环。初始化代码(重置)位于两个循环之间

done = False
while not done:
    # initialize program
    reset = False

    #Keep looping for commands until the user wants to reset or quit
    while not (done or reset):
        ...
        if taskNum == 6:
            done = True
            ...
        elif taskNum == 7:
            reset = True
            ...

正如ggorlen所提到的,如果您只想结束函数中的任何进一步操作,那么将if tasknum==6替换为以下代码:

        if taskNum == 6:
            return


就是这样。如果用户输入6,这些行将导致程序返回到调用它的main行正下方的行。

请提供预期的(MRE)。相反,您为一个10行的问题发布了60行代码。这对以后的访问者来说是无用的堆栈溢出。欢迎访问SO<代码>中断仅中断内部循环。你想打破所有的循环吗?如果是,只需从函数返回
。总的来说,项目结构很差。递归调用
robotTaskManager
意味着您的程序可能会被不断重置程序直到调用堆栈崩溃的用户崩溃。您使用了不适当的递归。避免递归的一种方法是创建一个变量,如
done=False
,然后在未完成时循环
(当您想要终止循环时,在循环内部将其值设置为
True
)。这将返回到调用
robotTaskManager
的行之后的代码行,只有在第一次调用时才在
main
中。否则,它将返回到操作
7
下的递归调用。简言之,这并不能解决问题。当前代码
中断
的循环从函数,并执行战术返回。@sirmrgat:请参阅