Alright i am totaly new to python and could really use some help. I need a program that will run and starting at "2:00" count down all the way to "0:00". The left side being minutes and the right side being seconds. so 120 seconds total. I have no idea how to get it into this format or where to even start if someone could show me some example scripts or help me out in any way that would be great. Thanks

3 Years
Discussion Span
Last Post by rproffitt

You would want something like this:

for t in range(120,-1,-1):
    minutes = t / 60
    seconds = t % 60
    print "%d:%2d" % (minutes,seconds) # Python v2 only

... but that just prints out the time left. If you're supposed to put the time in a separate window, well, use the above as a start and replace the print with code to write to the window.


To get a nice display you need to use a GUI toolkit like the one that comes with your Python installation. Here is an example, study it carefully ...

''' tk_counter_down101.py
count down seconds from a given minute value
using the Tkinter GUI toolkit that comes with Python
tested with Python27 and Python33

    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk
import time

def count_down():
    # start with 2 minutes --> 120 seconds
    for t in range(120, -1, -1):
        # format as 2 digit integers, fills with zero to the left
        # divmod() gives minutes, seconds
        sf = "{:02d}:{:02d}".format(*divmod(t, 60))
        #print(sf)  # test
        # delay one second

# create root/main window
root = tk.Tk()

time_str = tk.StringVar()

# create the time display label, give it a large font
# label auto-adjusts to the font
label_font = ('helvetica', 40)
tk.Label(root, textvariable=time_str, font=label_font, bg='white', 
         fg='blue', relief='raised', bd=3).pack(fill='x', padx=5, pady=5)

# create start and stop buttons
# pack() positions the buttons below the label
tk.Button(root, text='Count Start', command=count_down).pack()
# stop simply exits root window
tk.Button(root, text='Count Stop', command=root.destroy).pack()

# start the GUI event loop

this is what i got

import time #Imports time module

def main():
    x = int(input("Enter number of seconds: "))
    for i in range(x + 1):
        x -= 1

def formatTime(x):
    minutes = int(x / 60)
    seconds_rem = int(x % 60)
    if (seconds_rem < 10):
        return(str(minutes) + ":0" + str(seconds_rem))
        return(str(minutes) + ":" + str(seconds_rem))


Nice effort!
See if you can increase your knowledge of Python this way ...

import time

def main():
    nsec = int(input("Enter number of seconds: "))
    # range from nsec to zero backwards
    for x in range(nsec, -1, -1):

def formatTime(x):
    minutes, seconds_rem = divmod(x, 60)
    # use string formatting with C type % specifiers
    # %02d means integer field of 2 left padded with zero if needed
    return "%02d:%02d" % (minutes, seconds_rem)


If you are not comfortable with ranging backwards you could also use
for x in reversed(range(nsec+1)):

Edited by vegaseat


Hello i would like to add Hours to the countdown

Edited by Catrudo


does anyone know how to create a program on counting down seconds until it gets to 0. Asks to import pygame, i am having trouble with this help

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.