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 trial

Python Dates and Times in Python (2014) Dates and Times Time Tango

Mary Yang
Mary Yang
1,769 Points

Python - combine two user inputs into a date and time datetime object and return them.

OK, I just ran through all this code in workspaces dealing with various issues until I got it all figured out. I was proud and happy as I pasted my newly crafted code into the challenge task and what do I get when I hit submit? "Bummer! Try again!". No useful error code, no explanation why my answer isn't acceptable. Just a lolnope!

This code works in workspaces and returns a datetime just like the question asks. Can someone please explain what's wrong?

Thanks

combo.py
import datetime

date = "11-16-1976"
time = "01:45"

def time_tango(date, time):
    return datetime.datetime.strptime("{}, {}".format(date, time), "%m-%d-%Y, %H:%M")


time_tango(date, time)

Edit: Can you also explain why %I wouldn't work for the hour but %H does? %I is supposed to be a "Hour (12-hour clock) as a zero-padded decimal number."

2 Answers

Mikael Enarsson
Mikael Enarsson
7,056 Points

The problem is that they want you to make a function that takes a time object and a date object and combine those. There is a really handy function for it, so it shouldn't be a problem, I think ^^

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Mikael Enarsson is exactly right. You're not making a datetime from strings, you're making it from a date object and a time object.