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.

2
  • You can set an alarm and do something in the callback... medium.com/@chamilad/… Commented Oct 30, 2021 at 8: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. Commented Oct 31, 2021 at 2:41

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

Comments

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!!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.