Python Forum
How do I create a timer that counts down?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I create a timer that counts down?
#1
Could some one tell me how to make a timer that counts down? I have no problem making one that counts up, but I am at a loss with trying to figure out how to make one count down. To start I create a list of numbers.
time = [2,0,0,0]
This represents 20:00.(I combine all the numbers in to one string and display it as text. I can't figure out how to make it count down. If I were to make it count up I would do this.
           time[3] += 1
           if time[3] == 10:
               time[2] += 1
               time[3] = 0
           if time[2] == 6:
               time[1] += 1
               time[2] = 0
           if time[1] == 10:
               time[0] += 1
               time[1] = 0
I have this in a loop that runs once every second. Then when time[0] == 6 I just stop the loop. But How would I make a timer that counts down?
Reply
#2
Just use the datetime module:

from datetime import datetime,timedelta
import time

start = "20:00:00"
dt = datetime.strptime(start,"%H:%M:%S")
while True:
    print(dt.time())
    dt = dt - timedelta(seconds=1)
    time.sleep(1)
Reply
#3
Subtract one from the smallest unit. If that results in -1, set that unit to the max, and subtract one from the next highest unit. When it's all zeros, you're done.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
time[3] -= 1
if time[3] < 0:
    time[3] = 9
    time[2] -= 1
if time[2] < 0:
    time[2] = 5
    time[1] -= 1
if time[1] < 0:
    time[1] = 9
    time[0] -= 1
if time[0] < 0:
    time[0] = 2
    time[1] = 3
Reply
#5
Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting column of values into muliple columns of counts highland44 0 205 Feb-01-2024, 12:48 AM
Last Post: highland44
  Trying to get counts/sum/percentages from pandas similar to pivot table cubangt 6 1,324 Oct-06-2023, 04:32 PM
Last Post: cubangt
  Read All Emails from Outlook and add the word counts to a DataFrame sanaman_2000 0 1,816 Sep-15-2022, 07:32 AM
Last Post: sanaman_2000
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,497 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  PyPi Download Counts and Warehouse Migration? tlee0818 1 1,251 Mar-20-2022, 07:41 PM
Last Post: snippsat
  email timer/rss feed timer ndiniz 1 2,041 Feb-02-2021, 07:18 PM
Last Post: nilamo
  How to map 360 degree angle over 1024 counts breadcat248 3 2,440 May-17-2019, 07:13 AM
Last Post: breadcat248

Forum Jump:

User Panel Messages

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