HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Countdown clock in Python

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
countdownpythonclock

Problem

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)

Solution

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"))
>>> 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 >= 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()

Code Snippets

print (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
>>> 2015-09-17 15:43:30
while chosen_time >= datetime.now():
    time_difference = (chosen_time - chosen_time.now())
    print str(time_difference) + ' until the time.'
    time.sleep(3)
print 'The time you chose is: {}'.format(chosen_time)
time_difference = chosen_time - chosen_time.now()
month = raw_input('What is the month of the date? ').strip()

Context

StackExchange Code Review Q#104929, answer score: 8

Revisions (0)

No revisions yet.