0

I want to a timer inside my python program, which keeps running while the program is running and once timer is over, it displays a message or performs an action like breaking a loop or continuing the loop.

  • You can set an alarm and do something in the callback... https://medium.com/@chamilad/timing-out-of-long-running-methods-in-python-818b3582eed6 – Mark Setchell Oct 30 '21 at 08:49
  • With all due respect, the supposedly answer/solution `How can I time a code segment for testing performance with Pythons timeit?` is not what Walnut street is asking. – ferdy Oct 31 '21 at 02:41

2 Answers2

1

You can use threads to execute a timer while the program is running. Here is a explanation to threads: https://realpython.com/intro-to-python-threading/

The Reider
  • 143
  • 6
0

You can use this code:

import time
from random import random

timer = int(input("Enter time: "))
while True:
    text = random.randint(1, 10)
    time.sleep(timer)
    break
print(text)

You can always change what you wanna do inside the while loop.

Hope it worked!!

Swagrim
  • 422
  • 4
  • 17