Simple Stopwatch in python using time.sleep

Question:

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.

Asked By: ThisGuyThatGuy

||

Answers:

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.

Answered By: EvilSmurf
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.