Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Dates and Times in Python Dates and Times Time Tango

David Jones
David Jones
2,977 Points

I've attempted writing this function 2 different ways and both ways fail in the challenge task, but pass in PyCharm

Both of these functions work in PyCharm but fail in the challenge on Treehouse

from datetime import datetime

def time_tango(date1, time1):
    dt = datetime.strptime(f'{date1} {time1}', '%Y-%m-%d %H:%M:%S')
    return dt
from datetime import datetime, date, time

def time_tango(date1, time1):
    year = date1.year
    month = date1.month
    day = date1.day
    hour = time1.hour
    minute = time1.minute
    seconds = time1.second
    dt = datetime(year, month, day, hour, minute, seconds)
    return dt
combo.py
from datetime import datetime

def time_tango(date1, time1):
    dt = datetime.strptime(f'{date1} {time1}', '%Y-%m-%d %H:%M:%S')
    return dt

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 66,739 Points

Hey David Jones, The short answer is you are be correct! The issue is with the challenge checker.

For your case, import just the top level module datetime then use datetime.datetime

Also fixed to include microsecond

This fails:

#import datetime
from datetime import datetime

def time_tango(date1, time1):
    year = date1.year
    month = date1.month
    day = date1.day
    hour = time1.hour
    minute = time1.minute
    seconds = time1.second
    microsecs = time1.microsecond  # ADD
    # calling class directly 
    dt = datetime(
        year, month, day, hour, 
        minute, seconds, microsecs)
    return dt

This passes ๐Ÿ‘

import datetime
# from datetime import datetime, date, time

def time_tango(date1, time1):
    year = date1.year
    month = date1.month
    day = date1.day
    hour = time1.hour
    minute = time1.minute
    seconds = time1.second
    microsecs = time1.microsecond  # ADD
    # calling through module namespace 
    dt = datetime.datetime(
        year, month, day, hour, 
        minute, seconds, microsecs)
    return dt

Marking as feedback to devs.

Post back if you need more help. Good luck!!!

David Jones
David Jones
2,977 Points

I realized I was leaving out the purpose of this assignment which was to use combine()

Chris Freeman
Chris Freeman
Treehouse Moderator 66,739 Points

Perhaps so, but the combine method is not โ€œrequiredโ€ to pass.

Chris Freeman
Chris Freeman
Treehouse Moderator 66,739 Points

...and you may have uncovered a bug in the challenge checker! So +1 ๐Ÿ‘