0

I'm wondering if I could make something similar to the quit command but instead of ending the script it restarts it from line 1.

Example:

def restart():
   #Something that would repeat the whole script.

answer = input("Test")
if answer = "Restart":
    restart()
new Q Open Wid
  • 2,225
  • 2
  • 18
  • 34
SecretLloyd
  • 109
  • 1
  • 13

2 Answers2

1

As said, one way to do this is to make an infinite loop, using while True: or something else.

But if you want to make it into a function you have to do this:

import os
import sys

def restart():
    os.execl(sys.executable, sys.executable, *sys.argv)

For example:

import time
import os
import sys

def restart():
    os.execl(sys.executable, sys.executable, *sys.argv)

print("Test")
time.sleep(1)
restart()
new Q Open Wid
  • 2,225
  • 2
  • 18
  • 34
0

The typical way to do this would be to write a function, and run it forever in a loop.

Jussi Nurminen
  • 2,257
  • 1
  • 9
  • 16