Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Imperfect Stopwatch
#1
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)
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error when trying to run a stopwatch nath2125 1 1,868 Feb-23-2019, 09:14 PM
Last Post: Yoriz
  stopwatch output to txt file robertofreemano 6 3,817 Jul-18-2018, 07:59 AM
Last Post: robertofreemano
  Stopwatch Panda 1 2,176 Jul-11-2018, 10:21 PM
Last Post: micseydel
  Stopwatch in Python/IPython shell suvadip 3 4,207 Jul-06-2018, 01:51 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020