Here is my goal: To make a small program (text based) that will start with a greeting, print out a timer for how long it has been since the last event, and then a timer for the
On my PC (Windows 7) when run in a cmd
window, this program works almost exactly as you say it should. If the timer is repeating on a new line with each second, that suggests to me that os.system ('cls')
is not working for you -- perhaps because you're running on an OS other than Windows?
The statement while s<=60:
appears to be incorrect because s
will never be equal to 60 in that test -- anytime it gets to 60, it is reset to 0 and m
is incremented. Perhaps the test should be while m<60:
?
Finally, on my PC, the timer does not appear to lag behind actual seconds on the clock by much. Inevitably, this code will lag seconds on the clock by a little -- i.e. however long it takes to run all the lines of code in the while
loop apart from time.sleep(1)
, plus any delay in returning the process from the sleeping state. In my case, that isn't very long at all but, if running that code took (for some reason) 0.1 seconds (for instance), the timer would end up running 10% slow compared to wall clock time. @sberry's answer provides one way to deal with this problem.