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

Bill Leddy
Bill Leddy
2,720 Points

combo.py - what are the parameters?

I assumed the time and date parameters are time and date objects. I used the date time.combine method but no joy.

Also tried them as datetime objects, but that didn't work either.

combo.py
from datetime import datetime, time, date

def time_tango(theDate, theTime):
    return datetime.combine(date(theDate.year,theDate.month,theDate.day),
                            time(theTime.hour,theTime.minute,theTime.second,theTime.microsecond))

4 Answers

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Bill,

The issue seems to be that the challenge doesn't want you to bring date, time, datetime into the namespace. So instead of:

from datetime import datetime, date, time

Try just:

import datetime

Then prefixing each of your references to datetime classes with datetime.

I did this with your code and was able to pass the challenge.

Cheers

Alex

Aldo Rivadeneira
Aldo Rivadeneira
3,241 Points

Hi! Im trying to understand this exercise, because i was trying to do something like this.

def combine_time(dt, ht):
    format_date = datetime.datetime.strptime(dt, "%d/%m/%Y")
    format_hour = datetime.datetime.strptime(ht, "%H:%M")
    full_date_obj = datetime.datetime.combine(datetime.date(format_date), datetime.time(format_hour))
    return full_date_obj

Thanks in advance

Bill Leddy
Bill Leddy
2,720 Points

Truth be told I found this one confusing because I didn’t fully understand the nature of the inputs or the goal of the output.

I fudged it by constructing the output using strptime to simply extract the elements I wanted from the inputs to create a new datetime object. I doubt that is what he had in mind but it passed.

Sorry, I no longer have the code I used.

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Aldo,

You don't need to use strptime. You are given a date object and a time object as inputs to your function. You can use combine on them directly to create a datetime object.

Cheers

Alex

Lihua Yao
Lihua Yao
4,218 Points

but seems like it didn't say the input will be a time object and a date object. I tried to use combine directly, but still get error, and didn't say which kind of error it is. Kenneth Love

Lihua Yao
Lihua Yao
4,218 Points

I'm trying to understand this exercise too.

import datetime

def time_tango(date1, time1):
    return datetime.datetime.combine(datetime.date(date1), datetime.time(time1))

But I don't know why it just returns "Bummer: Try again!"