Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Simple Stopwatch in python using time.sleep

Im trying to create a simple stopwatch using counter logic and sleep function in python. It seems to increment fine and be pretty accurate with the one issue of my if statement.

self.sec = 0
self.min = 0

time.sleep(1)
self.sec = self.sec + 1
if (self.sec == 59):
    self.sec = 0
    self.min = self.min + 1


I’d like minutes to increment whenever seconds reach 59 as shown. Only problem is for a quick second these two things happen at the same time and reads the wrong time. For example, after 59 seconds it reads 1:59 and then reverts back to 1.1 and continues as normal.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

First thing: Weird, that your minute would only have 59 seconds instead of 60. Your example would result in the exact behavior described in your problem. Try changing to if (self.sec == 60): ... and check the results.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading