[Tutor] timer module question
Dick Moores
rdm at rcblue.com
Thu Jul 1 20:15:02 EDT 2004
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 = raw_input("Press Enter to start timer; enter 0 to close
program "
t0 = time.time()
if start == "0":
print "Timer has been shut down"
break
time_now = time.ctime()
print "Timer has been started at", time_now[11:19], "\n"
raw_input("Press Enter to stop timer ")
t1 = time.time()
elapsed_time = t1 - t0
time_now = time.ctime()
print "\nTimer has been stopped at", time_now[11:19]
print "Time elapsed was", elapsed_time, "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
Thanks,
Dick Moores
More information about the Tutor
mailing list