-3

I made this box of code and the loop goes on forever, I want the loop to go for 5 seconds then stop completely. (also import time and random are put in above in the code)

I haven't tried much, quite lost on how to do this.

def case1():
    start = 1
    while True:
        int = randint(0,3)
        if int < 1:
            print('Blue')
            sleep(0.05)

        elif int < 2:
            print('Purple')
            sleep(0.05)

        elif int <= 3:
            print('Pink')
            sleep(0.05)

I need the loop to stop, after 5 seconds

1 Answers1

0

To stop the loop after 5 seconds you can use time() from time:

from time import time

def case1():
    start = time() + 5
    while time() < start:
        num = randint(0,3)
        if num < 1:
            print("Blue")
            sleep(0.05)
        elif num < 2:
            print("Purple")
            sleep(0.05)
        elif num <= 3:
            print("Pink")
            sleep(0.05)
kreny
  • 18
  • 4