Skip to content Skip to sidebar Skip to footer

Python Restart The Program After Running A Method

A noob question I'm sure. For example, say I have a program that looks something like this. def method1(): #do something here def method2(): #do something here #this is t

Solution 1:

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 the endless loop "doesn't feel right", ask yourself when and why it should end. Should you have a third input option that exits the loop? Then add:

ifmenu== "3":
    break

Solution 2:

An endless loop is the way to do it though, something like this:

running = truedefmethod1(): 
   #do something heredefmethod2():
    #do something heredefstop():
    running = falsewhilerunning:#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()

Post a Comment for "Python Restart The Program After Running A Method"