[英]How to go back to a previous part of the code in Python?
I am working on a CLI application and using PyInquirer to create a select menu.我正在开发 CLI 应用程序并使用PyInquirer创建 select 菜单。 So what I have created is a select menu, which asks the user to choose what to do, the options are:所以我创建的是一个 select 菜单,它要求用户选择要做什么,选项有:
- view task,查看任务,
- Add Task,添加任务,
- Push up Task.推高任务。
I've created a function that when you select view task, it prompts another set of choices.我创建了一个 function,当您 select 查看任务时,它会提示另一组选择。 However, from here I'd like to return back to the main menu.但是,从这里我想回到主菜单。 I can't seem to wrap my head around the logic I need to do this.我似乎无法理解我需要这样做的逻辑。 I know I could hard-code all the possible paths but there must be a better way to do this.我知道我可以对所有可能的路径进行硬编码,但必须有更好的方法来做到这一点。 I thought about creating functions but the only way I could think of them is by calling each other which wouldn't work.我考虑过创建函数,但我能想到的唯一方法是互相调用,这是行不通的。
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
Thank you for any guidance or advice.感谢您的任何指导或建议。
You can wrap the whole thing in a while True:
statement, so that whenever you've finished with any of the pathways, you return to the main menu select.您可以将整个内容包装在while True:
语句中,这样每当您完成任何路径时,您就会返回到主菜单 select。 When you want to quit, just use break
to exit out of the loop.当您想退出时,只需使用break
退出循环即可。 Below is a quick example.下面是一个简单的例子。
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.