Python Forum

Full Version: Imperfect Stopwatch
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey, I'm new here so if I break any rules, feel free to scold me.

Anyway, I was learning how to program a simple timer from a YouTube video. I got everything to work, and tweaked a few things myself. But when I compared it with an independent timer (phone and computer timer), I noticed that it runs a bit slower. By the two minute mark it's about 15 seconds behind the independent timer. Is there a way to improve this or would I have to completely start from scratch to get a more accurate one?

Oh, and I'm running 3.7.

Here's the code:

import time
from os import system

seconds = int(0)
minutes = int(0)
hours = int(0)

run = input("Enter R to run the program\n")

while run.lower()=="r":
    seconds = (seconds + 1)
    if seconds > 59:
        seconds = 0
        minutes += 1
    
    if minutes > 59:
        minutes = 0
        hours += 1
    system('cls')
    print(hours,":", minutes,":",seconds)
    time.sleep(1)
Well, you're waiting a second, then doing some stuff, and then waiting a second, and doing stuff, ... Doing stuff takes time. Printed output especially takes time. So it's going to be slower than an accurate stopwatch.