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 QOpenGLWidget
  • 1,744
  • 2
  • 15
  • 28
SecretLloyd
  • 99
  • 1
  • 12

2 Answers2

0

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

Jussi Nurminen
  • 2,025
  • 1
  • 7
  • 15
0

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 QOpenGLWidget
  • 1,744
  • 2
  • 15
  • 28