Search code examples
pythonrandomcountersleepnonetype

How to properly use sleep with print and random in python


I’m a novice and I have been trying to build a random countdown timer which picks a number between 0 and 10, and counts to zero from the selected integer. All while printing the countdown simultaneously. However, I keep getting errors from sleep().

import random
import time

x = random.randint(0,10)

y = time.sleep(x)

while y != 0:
    print(y)

Solution

  • This might be help you:

    import random
    import time
    
    countdown = random.randint(0,10)
    
    for i in reversed(range(countdown)):
        print(str(i) + ' sec left')
        time.sleep(1)