[Tutor] timer module question (original) (raw)

Chad Crabtree flaxeater at yahoo.com
Fri Jul 2 00:24:38 EDT 2004


--- Dick Moores <rdm at rcblue.com> wrote:

Today I've been studying the time module using the Python Library Reference. As an exercise I thought I'd make a timer script.

What I'd like to get it to do in addition, is tell me every 10 seconds how many seconds have elapsed while the timer is running. Is there a way to do this? Here's what I have. It seems to work well: ===================================== #timer.py import time while True: start = rawinput("Press Enter to start timer; enter 0 to close program " t0 = time.time() if start == "0": print "Timer has been shut down" break timenow = time.ctime() print "Timer has been started at", timenow[11:19], "\n" rawinput("Press Enter to stop timer ") t1 = time.time() elapsedtime = t1 - t0 timenow = time.ctime() print "\nTimer has been stopped at", timenow[11:19] print "Time elapsed was", elapsedtime, "seconds" print "-------------------------------------\n" ====================================== sample output: Press Enter to start timer; enter 0 to close program Timer has been started at 16:37:48 Press Enter to stop timer Timer has been stopped at 16:40:56 Time elapsed was 188.156000137 seconds ------------------------------------- Press Enter to start timer; enter 0 to close program ===================================== (Later I'll want to trim the accuracy of the result, and also convert to hours, minutes, seconds, but I think I'll be able to figure that out. Being able to turn this into a GUI timer is way down the road for me.) If it matters, I'm running Python 2.3.4 on Windows XP Pro Must this be stored by ascii? if not try looking at the pickle module.


Do you Yahoo!? Yahoo! Mail - 50x more storage than other providers! http://promotions.yahoo.com/new_mail



More information about the Tutor mailing list