Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python在运行方法后重新启动程序_Python - Fatal编程技术网

python在运行方法后重新启动程序

python在运行方法后重新启动程序,python,Python,我敢肯定这是个很难回答的问题。 例如,假设我有一个类似这样的程序 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"

我敢肯定这是个很难回答的问题。 例如,假设我有一个类似这样的程序

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

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
如果无休止的循环“感觉不对劲”,问问自己它应该在什么时候结束,为什么结束。是否应该有第三个退出循环的输入选项?然后添加:

if menu == "3":
    break

但是,一个无止境的循环是实现这一点的方法,类似这样:

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()

但是,一个无止境的循环是实现这一点的方法,类似这样:

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()

我会用一个无止境的循环(或者不是无止境的,取决于你在做什么)。如果有退出程序的选择,我会用一个无止境的循环(或者不是无止境的,取决于你在做什么)。如果有退出程序的选择,则将其包装为非无限循环。Russels方法优于此Russels方法优于此感谢这太棒了。我想把整个事情都放在循环中,方法和一切都很好。。。你永远不会真的想把“整个程序”包装成一个循环;您希望将需要循环的零件包装成一个循环。:)谢谢,这太好了。我想把整个事情都放在循环中,方法和一切都很好。。。你永远不会真的想把“整个程序”包装成一个循环;您希望将需要循环的零件包装成一个循环。:)