简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM