Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJeff Muday
Treehouse Moderator 28,722 PointsNo specification on time/date string format given in challenge.
Dates and Times challenge 'time-tango'
My code works in workspaces, but does not pass the code-challenge. What the heck did the designers intend for time/date strings we should accept?
My code tests fine in the YYYY-mm-dd and 24-hour HH:MM format.
Should this challenge should be re-written with more specifications or am I just an idiot?
import datetime
def time_tango(date,time):
# assumes date format YYYY-MM-DD and time format 24-hour HH:MM
timestr = "{} {}".format(date,time)
return datetime.datetime.strptime(timestr, '%Y-%m-%d %H:%M')
# testing
date = '2017-01-01'
time = '15:30'
# Equivalent representation, datetime
this_datetime = datetime.datetime(2017,1,1,15,30)
if time_tango(date,time) == this_datetime:
print ('time_tango PASS')
else:
print ('time_tango FAIL')
1 Answer
Christopher Shaw
Python Web Development Techdegree Graduate 58,248 PointsYou need to use combine: https://docs.python.org/3/library/datetime.html#datetime.datetime.combine
return datetime.datetime.combine(date, time)
Jeff Muday
Treehouse Moderator 28,722 PointsJeff Muday
Treehouse Moderator 28,722 PointsThanks -- that saves a few keystrokes. I might simply returned a datetime.datetime built from the component parameters of the input.
That version looks more convoluted (as shown below).