Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I restart a program based on user input?

Tags:

python

I'm trying to restart a program using an if-test based on the input from the user.

This code doesn't work, but it's approximately what I'm after:

answer = str(raw_input('Run again? (y/n): '))

if answer == 'n':
   print 'Goodbye'
   break
elif answer == 'y':
   #restart_program???
else:
   print 'Invalid input.'

What I'm trying to do is:

  • if you answer y - the program restarts from the top
  • if you answer n - the program ends (that part works)
  • if you enter anything else, it should print 'invalid input. please enter y or n...' or something, and ask you again for new input.

I got really close to a solution with a "while true" loop, but the program either just restarts no matter what you press (except n), or it quits no matter what you press (except y). Any ideas?

like image 992
MagnusIngemann Avatar asked Feb 16 '13 04:02

MagnusIngemann


People also ask

How do you auto restart a program in Python?

Now, in a Python Shell, you would have to press either the Run button and then 'Run Module (F5)' or just the F5 key on your keyboard. That is the first time you run it. When the program ended, you would go back to your Cheese.py file and then press F5 to run the program again.

Is there a function to restart the program in Python?

Restart the Program Script in Python Using the os. execv() Function.

How do you rerun code in Python?

You need to bury the code block in another code block. The program runs once within main() variable then exits and returns to run the main varible again. Repeating the game. Hope this helps.


6 Answers

This line will unconditionally restart the running program from scratch:

os.execl(sys.executable, sys.executable, *sys.argv)

One of its advantage compared to the remaining suggestions so far is that the program itself will be read again.

This can be useful if, for example, you are modifying its code in another window.

like image 137
jlliagre Avatar answered Sep 21 '22 03:09

jlliagre


Try this:

while True:
    # main program
    while True:
        answer = str(input('Run again? (y/n): '))
        if answer in ('y', 'n'):
            break
        print("invalid input.")
    if answer == 'y':
        continue
    else:
        print("Goodbye")
        break

The inner while loop loops until the input is either 'y' or 'n'. If the input is 'y', the while loop starts again (continue keyword skips the remaining code and goes straight to the next iteration). If the input is 'n', the program ends.

like image 21
Volatility Avatar answered Sep 23 '22 03:09

Volatility


Using one while loop:

In [1]: start = 1
   ...: 
   ...: while True:
   ...:     if start != 1:        
   ...:         do_run = raw_input('Restart?  y/n:')
   ...:         if do_run == 'y':
   ...:             pass
   ...:         elif do_run == 'n':
   ...:             break
   ...:         else: 
   ...:             print 'Invalid input'
   ...:             continue
   ...: 
   ...:     print 'Doing stuff!!!'
   ...: 
   ...:     if start == 1:
   ...:         start = 0
   ...:         
Doing stuff!!!

Restart?  y/n:y
Doing stuff!!!

Restart?  y/n:f
Invalid input

Restart?  y/n:n

In [2]:
like image 44
root Avatar answered Sep 22 '22 03:09

root


You can do this simply with a function. For example:

def script():
    # program code here...
    restart = raw_input("Would you like to restart this program?")
    if restart == "yes" or restart == "y":
        script()
    if restart == "n" or restart == "no":
        print "Script terminating. Goodbye."
script()

Of course you can change a lot of things here. What is said, what the script will accept as a valid input, the variable and function names. You can simply nest the entire program in a user-defined function (Of course you must give everything inside an extra indent) and have it restart at anytime using this line of code: myfunctionname(). More on this here.

like image 30
It's Willem Avatar answered Sep 21 '22 03:09

It's Willem


Here's a fun way to do it with a decorator:

def restartable(func):
    def wrapper(*args,**kwargs):
        answer = 'y'
        while answer == 'y':
            func(*args,**kwargs)
            while True:
                answer = raw_input('Restart?  y/n:')
                if answer in ('y','n'):
                    break
                else:
                    print "invalid answer"
    return wrapper

@restartable
def main():
    print "foo"

main()

Ultimately, I think you need 2 while loops. You need one loop bracketing the portion which prompts for the answer so that you can prompt again if the user gives bad input. You need a second which will check that the current answer is 'y' and keep running the code until the answer isn't 'y'.

like image 28
mgilson Avatar answered Sep 23 '22 03:09

mgilson


It is very easy do this

while True:

       #do something

       again = input("Run again? ")

       if 'yes' in again:
              continue
       else:
              print("Good Bye")
              break

Basically, in this the while loop will run the program again and again because while loops run if the condition is True so we have made the condition true and as you know True is always true and never false. So, will not stop then after this the main part comes here first we will take input from the user whether they want to continue the program or not then we will say that if user says yes i want to continue then the continue keyword will bring the loop to the top again and will run the program again too and if the user says something else or you can do it in another way if you want to only quit the program if user says no then just add this

elif 'no' in again:
       print("Good Bye")
       break
else:
       print("Invalid Input")

this will look that if there is the 'no' word in the input and if there is then it will break the loop and the program will quit

like image 36
PrabhavDevo Avatar answered Sep 25 '22 03:09

PrabhavDevo