简体   繁体   中英

Restarting a script in Python

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()

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

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM