Countdown clock in Python This project is from the subreddit for beginner Python projects:


  The goal of the project is to create a program that allows the user to choose a time and date, and then prints out a message at given intervals (such as, every 3 seconds) that tells the user how much longer there is until the selected time. If the chosen time has passed, the program lets the user know. When selecting the month, the user has the option of entering the month name or number.


Is there a simple and succinct way to remove the microseconds from being printed out? And is there a better way to set up the while loop?

import datetime
import time

# a simple program that allows the user to pick a time and the program counts down
# until that time has been reached

month_strings = ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec',]

# This function creates a datetime object chosen by the user and compares
# this object to the current time and counts down until that chosen time
def count_down():


    year = int(raw_input('What is the year of the date? '))
    month = raw_input('What is the month of the date? ')
    # this part allows the user to type in the month as a string or integer
    if len(month) &lt; 3:
        month = int(month)
    else:   
        month = (month_strings.index(month[0:3].lower()) + 1)
    day = int(raw_input('What is the day of the month? '))
    hour = int(raw_input('What is the hour according to a 24 hour clock? '))
    minute = int(raw_input('At which minute? '))
    second = int(raw_input('At which second? '))

    chosen_time = datetime.datetime(year,month,day,hour,minute,second)

    print 'The time you chose is: ' + chosen_time.__str__()

    current_time = chosen_time.now()
    # If the time has passed, let the user know and exit the program
    if chosen_time &lt; chosen_time.now():
        print 'This time has passed.'
        exit()

    else:
        # This loop prints how much time is left until the chosen time every 3 seconds
        while True:
            time_difference = (chosen_time - chosen_time.now())
            print str(time_difference) + ' until the time.'
            time.sleep(3)
            if chosen_time &lt;= chosen_time.now():
                break
        print "It's time!"
        exit()

if __name__ == '__main__':  
    count_down()
I'll first just suggest that you read the Python style guide as it has great tips on readability. 

The best way you can format time is using datetime.strftime. You can call it on a datetime object and pass a string formatted to represent each time value and it will format the data into the string format you provide. This way you can format an exact code you'd like instead of just removing milliseconds:

print (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
&gt;&gt;&gt; 2015-09-17 15:43:30


Using the table here you can see all the available options if you'd like to go more specific. While I'm on the datetime, it is not clear when you call .now on your stored object. It reads awkwardly. Instead use datetime.datetime.now(). But, since that's a lot to type I'd instead suggest calling from datetime import datetime at the start, so you can then just call datetime.now().

On your second question, yes there is a better way to do the while loop. Instead of using if to check conditions and break on them, make that condition the condition of your while loop.

while chosen_time &gt;= datetime.now():
    time_difference = (chosen_time - chosen_time.now())
    print str(time_difference) + ' until the time.'
    time.sleep(3)


The reason people usually use while True is that either the condition is not easy to make into an expression, the condition can't be evaluated when the loop is firt encountered or that they want to emulate a do while loop. But none of these is true for you, so it's not needed.

Other Notes

You almost never need to call __str__. The point of functions with two underscores on either side is that they allow on object to be passed to another function and run that way. In this case, you could use str(chosen_time). But I wouldn't use that either, instead use str.format.

print 'The time you chose is: {}'.format(chosen_time)


It replaces the {} with the parameter you pass into format. It's very useful when you have multiple parameters or need to apply particular formatting to them, so you should get used to using it.

You don't need brackets around the `time_difference calculation, so leave them out.

time_difference = chosen_time - chosen_time.now()


I agree with Ethan about the input validation, but I want to specifically note that someone could enter "12 ". That would seem too long to be an integer, but would actually require that you call int on it, which you currently don't. I suggest that you call strip on the month parameter. It will remove whitespace at the start and end of the user's input, preventing this issue.

month = raw_input('What is the month of the date? ').strip()