Home > Software design >  Is there a way to make a python program wait a few seconds? I am making an infinite number printer b
Is there a way to make a python program wait a few seconds? I am making an infinite number printer b

Time:12-15

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?

CodePudding user response:

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

CodePudding user response:

thx guys for the help

thanks to NejoFs, it works now, the code looks like this:

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 'b' 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)

CodePudding user response:

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)
  • Related