Application requirements: In Python, while True keeps running the code in a loop, and the statement in the if needs to be executed every 2s (or n seconds)——implementation method (cannot use time.sleep) (tested, available)

1. Before the While True loop, define the initial time variable t0 and an intermediate variable b in main

 2, the second step, see the specific code

 (Hint: The statement in the if here is simply represented by print("a"))

code:

def main():

    t0 = time.time()
    b = 0

while True

       t1=time.time()

        t=int(t1-t0) # 1s a jump
        a=t

        if a>b:         
            print("a")
            b=a+1 # execute a print every 2s

Guess you like

Origin blog.csdn.net/P13643822101/article/details/129985690