我正在编写一个控制台应用程序,每次用户键入“退出”时都应关闭该应用程序。它仅在启动函数被调用一次时才有效,否则我必须多次输入“退出”。
例如:我运行应用程序,输入“1”、“食谱”、“退出”,它运行良好。当我输入“1”、[enter]、“2”、“exit”时——我需要再输入一次“exit”......或更多次(取决于函数调用次数)。
def start():
running = 'on'
while running == 'on':
recipes.show_recipes()
recipe_choice = input("...")
if recipe_choice == "exit":
running = 'off'
else:
try:
recipes.show_recipes(int(recipe_choice))
except IndexError:
print("...")
start()
except ValueError:
print("...")
start()
response = input("...")
if response == "recipe":
#opening recipe .pdf file
openrecipe(list(list(recipes.recipes.values())[int(recipe_choice)-1].values())[2], shell=True)
start()
elif response == "exit":
running = 'off'
else:
start()
start()
我想这个解决方案很简单,但我不明白为什么我的应用程序会这样。我想在控制台中输入“退出”的任何时候关闭程序。
您不需要一遍又一遍地调用 start() - 一旦您的程序到达其中一个 if 子句的末尾,它应该从 while 循环的开头重新开始。否则,您将启动必须手动退出的新循环。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句