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

Function returns a combined datetime object, but I'm still getting the wrong answer.

This is the way I did it, unless maybe there's an alternate method using operators? Or maybe the solution is looking for something else? I even ran an isinstance test on combined_date and datetime came back true. Not really sure what else to do here.

combo.py
from datetime import datetime


def time_tango(user_date, user_time):
    combined_date = datetime.combine(user_date, user_time)

    return combined_date
Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Devs: this code should pass. The level of import should not matter in this case. Please review checker behavior.

2 Answers

boi
boi
14,242 Points

You got two errors. Let us analyze.

from datetime import datetime πŸ‘ˆ# what is the first datetime? you only need import datetime


def time_tango(user_date, user_time):
    combined_date = datetime.combine(user_date, user_time)πŸ‘ˆ# datetime.combine is a wrong package

    return combined_date

datetime.combine() is a wrong pacakage or datetime don't have the extension of combine(), check it yourself.

try this in the REPL;

>>> import datetime

>>> dir(datetime)
['MAXYEAR', 'MINYEAR', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__'
, 'date', 'datetime', 'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo']  
>>> 
>>> dir(datetime.datetime)
['__add__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', 
'__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rsub__
', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', 'astimezone',πŸ‘‰  'combine' πŸ‘ˆ, 'ctime', 'date', 'day', 'dst', 'fold', 
'fromisocalendar', 'fromisoformat', 'fromordinal', 'fromtimestamp', 'hour', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'microsecond'
, 'min', 'minute', 'month', 'now', 'replace', 'resolution', 'second', 'strftime', 'strptime', 'time', 'timestamp', 'timetuple', 'timetz', 
'today', 'toordinal', 'tzinfo', 'tzname', 'utcfromtimestamp', 'utcnow', 'utcoffset', 'utctimetuple', 'weekday', 'year'] 
>>>

As shown, there is no combine() extension with datetime, it should be datetime.datetime.combine().

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

I think there’s more going on here. All of these should work equally as well:

# as module import
>>> import datetime
>>> help(datetime.datetime.combine)
Help on built-in function combine:

combine(...)
    date, time -> datetime with same date and time fields

# as renamed class import
>>> from datetime import datetime as dt
>>> help(dt.combine)
Help on built-in function combine:

combine(...)
    date, time -> datetime with same date and time fields

# as direct class import
>>> from datetime import datetime
>>> help(datetime.combine)
Help on built-in function combine:

combine(...)
    date, time -> datetime with same date and time fields

This needs further review.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Using the REPL example:

>>> from datetime import datetime
>>> dir(datetime)
[β€˜__add__’, β€˜__class__’, β€˜__delattr__’, β€˜__dir__’, β€˜__doc__’, β€˜__eq__’, β€˜__format__’, β€˜__ge__’, β€˜__getattribute__’, β€˜__gt__’, β€˜__hash__’, β€˜__init__’, β€˜__le__’, β€˜__lt__’, β€˜__ne__’, β€˜__new__’, β€˜__radd__’, β€˜__reduce__’, β€˜__reduce_ex__’, β€˜__repr__’, β€˜__rsub__’, β€˜__setattr__’, β€˜__sizeof__’, β€˜__str__’, β€˜__sub__’, β€˜__subclasshook__’, β€˜astimezone’, πŸ‘‰'combine'πŸ‘ˆ, β€˜ctime’, β€˜date’, β€˜day’, β€˜dst’, β€˜fromordinal’, β€˜fromtimestamp’, β€˜hour’, β€˜isocalendar’, β€˜isoformat’, β€˜isoweekday’, β€˜max’, β€˜microsecond’, β€˜min’, β€˜minute’, β€˜month’, β€˜now’, β€˜replace’, β€˜resolution’, β€˜second’, β€˜strftime’, β€˜strptime’, β€˜time’, β€˜timestamp’, β€˜timetuple’, β€˜timetz’, β€˜today’, β€˜toordinal’, β€˜tzinfo’, β€˜tzname’, β€˜utcfromtimestamp’, β€˜utcnow’, β€˜utcoffset’, β€˜utctimetuple’, β€˜weekday’, β€˜year’]
>>> 

Sweet, thanks. I'm using my own IDE and the command prompt and have different imports for both, so it was probably getting confused.