While timer in Python

Asked

Viewed 1,956 times

4

How to make a timer in while? I would like to run a while, and do something, e.g. a

print "hello" 

run print "hello" for 1 hour for example.

1 answer

3


You can do it like this:

import time

end_time = time.time() + 10
countTimer = 0
sleepTime = 0.500
while time.time() < end_time:
    time.sleep(sleepTime)
    countTimer += sleepTime
    print('hello, ja passaram {} secs'.format(countTimer))

Change the 10 by 3600 for a 1hour. You can remove the Sleep, but the program will get very heavy

  • Perfect Miguel, that’s right! What if I wanted to show that this was being executed? Running the above code does not notice on the screen that hello is being printed as time goes by, due to the fact that nothing changes. Let’s say something like this: 500ms past it shows one: 500ms have passed. How could you do?

  • 1

    Ha ok I’ll do it, I get it

  • 1

    Made on top @Allan

  • Miguel ball show, incredible Kra ;-) congratulations!

  • You’re welcome @Allan. obgado

Browser other questions tagged

You are not signed in. Login or sign up in order to post.