简体   繁体   中英

Is there a way to make a python program wait a few seconds? I am making an infinite number printer but it's too fast to read

I don't know how to make my program wait a few seconds so I can actually read it Is there a wait function in python or is there a module for such? I dont have a way to run it in a window because I'm on a school chromebook.

My Code:


from random import randint

while True:
    a = randint(0,999999)
    b = randint(0,999999)
    c = randint(0,999999)
    
    if (a <= b) or (a <= c):
        print("variable 'a' has been printed")
        print(a)
        
    elif (b <= a) or (b <= c):
        print("variable 'c' has been printed")
        print(b)
        
    elif (c <= a) or (c <= b):
        print("variable 'c' has been printed")
        print(c)
    
    elif (a == b):
        print("Combo of 'a' + 'b'")
        print(a + b)
        
    elif (a == c):
        print("Combo of 'a' + 'c'")
        print(a + c)
    
    elif (b == c):
        print("Combo of 'b' + 'c'")
        print(b + c)


How to make it wait?

use sleep(), it takes its arguments in seconds.

from random import randint
from time import sleep
while True:
    a = randint(0,999999)
    b = randint(0,999999)
    c = randint(0,999999)

    
    if (a <= b) or (a <= c):
        print("variable 'a' has been printed")
        print(a)
        sleep(1)
        
    elif (b <= a) or (b <= c):
        print("variable 'c' has been printed")
        print(b)
        sleep(1)
        
    elif (c <= a) or (c <= b):
        print("variable 'c' has been printed")
        print(c)
        sleep(1)
    
    elif (a == b):
        print("Combo of 'a' + 'b'")
        print(a + b)
        sleep(1)
        
    elif (a == c):
        print("Combo of 'a' + 'c'")
        print(a + c)
        sleep(1)
    
    elif (b == c):
        print("Combo of 'b' + 'c'")
        print(b + c)
        sleep(1)

You'll need to import it from time as I have above.

Alternatively, it will be more efficient and maintainable if you have just one sleep() at the end of the while loop instead of several sleep() fucntions. Below is a much better example of the code above. Thank you to Max for the suggestion.

from random import randint
from time import sleep
while True:
    a = randint(0,999999)
    b = randint(0,999999)
    c = randint(0,999999)


    if (a <= b) or (a <= c):
        print("variable 'a' has been printed")
        print(a)
    
    elif (b <= a) or (b <= c):
        print("variable 'c' has been printed")
        print(b)
    
    elif (c <= a) or (c <= b):
        print("variable 'c' has been printed")
        print(c)

    elif (a == b):
        print("Combo of 'a' + 'b'")
        print(a + b)
    
    elif (a == c):
        print("Combo of 'a' + 'c'")
        print(a + c)

    elif (b == c):
        print("Combo of 'b' + 'c'")
        print(b + c)

    sleep(1)

You can introduce sleep in your code wherever you need to wait.

import time

time.sleep(5) # sleep for 5 seconds

Link to the documentation time.sleep()

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