一个菜鸟问题我敢肯定。 例如,假设我有一个看起来像这样的程序。
def method1():
#do something here
def method2():
#do something here
#this is the menu
menu=input("What would you like to do?\ntype 1 for method1 or 2 for method2: ")
if(menu=="1"):
method1()
if(menu=="2"):
method2()
如何在方法完成后再次显示此菜单而不是程序终止?
我以为我可以将整个程序包装成无限循环,但感觉不对:P
答案 0 :(得分:5)
while True:
#this is the menu
menu=input("What would you like to do?\ntype 1 for method1 or 2 for method2: ")
if(menu=="1"):
method1()
if(menu=="2"):
method2()
如果无限循环“感觉不对”,请问自己何时以及为何应该结束。你有第三个输入选项退出循环吗?然后添加:
if menu == "3":
break
答案 1 :(得分:0)
无限循环是这样做的方式,如下所示:
running = true
def method1():
#do something here
def method2():
#do something here
def stop():
running = false
while running:
#this is the menu
menu=input("What would you like to do?\ntype 1 for method1 or 2 for method2 (3 to stop): ")
if(menu=="1"):
method1()
if(menu=="2"):
method2()
if(menu=="3"):
stop()