0

I'm wondering how I can restart a script without just calling the function itself. You can see the example below. After "two" is printed I want the script to restart itself without just calling one().

import time
zero = 0

def one():
    global zero

    for i in range(50):
        time.sleep(0.5)
        zero += 1
        print(zero)
        if zero == 10:
            two()

def two():
    print("two")
    #Restart the script

one()
4
  • 1
    What do you mean by restart? What state are you expecting? What behavior? Commented Jul 14, 2019 at 15:45
  • Put the function call in a while loop. Such constructs are covered by tutorials Commented Jul 14, 2019 at 15:45
  • if you want it to "restart" after printing "two", why is it inside a loop? Maybe what you mean is if zero % 10 == 0:? Commented Jul 14, 2019 at 15:48
  • I need this example for an other script but it's too hard to explain with my English. I just want the code to stop after print("two") and restart everything again. I'm looking for something like os.execl(sys.executable, sys.executable, * sys.argv) Commented Jul 14, 2019 at 15:52

2 Answers 2

3

You want to do some condition forever, so the most practical way is to use a while loop with a condition that is always true.

while True:
    one()

You probably also want to return from function one after calling two

if zero == 10:
    two()
    return
Sign up to request clarification or add additional context in comments.

Comments

0

You can try with a while loop,

import time
zero = 10
i = 0
while i <= zero:
        time.sleep(0.5)
        zero += 1
        print(zero)
        print("two")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.