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 trialAnna Carney
1,675 PointsCode works in text editor, but not accepted for challenge question
The challenge question is to:
- Create a function called time_tango
- Function should take 2 arguments, date and time
- It should combine these arguments and return a datetime
I thought I hit all the requirements, and when I run it in my text editor, it returns a datetime without a problem. I've tried returning a datetime object and a datetime string, but neither are accepted. It only responds with "Bummer: Try again!". I'm at a point where I don't know what else to try.
Here's my code: '''python import datetime
def time_tango(date, time): return datetime.datetime.strptime(date + ' ' + time, '%Y-%m-%d %H:%M') '''
import datetime
def time_tango(date, time):
return datetime.datetime.strptime(date + ' ' + time, '%Y-%m-%d %H:%M')
2 Answers
Steven Parker
231,236 PointsThis would work if both arguments were strings, but the challenge is asking for a function that will take a date object and a time object as arguments.
So you could use strptime by first making strings out of the objects, or just construct a datetime object using the individual attributes of the arguments, but you might want to consider the easier approach using combine instead.
Jeff Muday
Treehouse Moderator 28,720 PointsYour solution and approach are very sound! However, this challenge wants something really specific -- the entire datetime including all the way down to seconds and microseconds.
For example...
>>> datetime.datetime.now()
datetime.datetime(2022, 4, 4, 17, 21, 27, 448892)
NOTE: this is left for you to solve, the combo
variable is what you should complete!
def time_tango(date, time):
combo = datetime.datetime(date.year, date.month, date.day, time.hour .....
return combo
Hope this helps!
Anna Carney
1,675 PointsAnna Carney
1,675 PointsThis is so much easier than the way I was thinking through the problem! Got it figured out now by using combine(). Thanks for your help!