繁体   English   中英

如何将 go 回到 Python 中的前一部分代码?

[英]How to go back to a previous part of the code in Python?

我正在开发 CLI 应用程序并使用PyInquirer创建 select 菜单。 所以我创建的是一个 select 菜单,它要求用户选择要做什么,选项有:

  1. 查看任务,
  2. 添加任务,
  3. 推高任务。

我创建了一个 function,当您 select 查看任务时,它会提示另一组选择。 但是,从这里我想回到主菜单。 我似乎无法理解我需要这样做的逻辑。 我知道我可以对所有可能的路径进行硬编码,但必须有更好的方法来做到这一点。 我考虑过创建函数,但我能想到的唯一方法是互相调用,这是行不通的。

def main():

main_menu_selection = prompt(main_menu, style=custom_style_2)
# Part I reference below
if main_menu_selection['which_task']== 'View Tasks':
    view_tasks_selection = prompt(view_tasks, style=custom_style_2)


if (view_tasks_selection['view_task']).lower() == 'daily':
    print_tasks("daily")
    # Function to return to menu
    print("Press Enter to return to Main Menu")
    user_input = input()
    if user_input:
        main_menu_selection = prompt(main_menu, style=custom_style_2)
        # Here I would need it to go back to the if statement mentioned above

感谢您的任何指导或建议。

您可以将整个内容包装在while True:语句中,这样每当您完成任何路径时,您就会返回到主菜单 select。 当您想退出时,只需使用break退出循环即可。 下面是一个简单的例子。

def main():

    while True:
        main_menu_selection = prompt(main_menu, style=custom_style_2)

        # Part I reference below
        if main_menu_selection['which_task']== 'View Tasks':
            view_tasks_selection = prompt(view_tasks, style=custom_style_2)


        if (view_tasks_selection['view_task']).lower() == 'daily':
            print_tasks("daily")

        else:
            break

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM