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 Let's Build a Timed Quiz App Harder Time Machine

Nicole A
Nicole A
1,790 Points

python date time module

"time data '10' does not match the format 'hours' ". I get this error message from the time_machine challenge. Could someone please tell me what's wrong with my code.

time_machine.py
import datetime

starter = datetime.datetime(2015, 10, 21, 16, 29)
def time_machine(integer,string):
    timedelta = datetime.datetime.strptime(f'{integer}',string)
    return timedelta + starter






# Remember, you can't set "years" on a timedelta!
# Consider a year to be 365 days.

## Example
# time_machine(5, "minutes") => datetime(2015, 10, 21, 16, 34)

2 Answers

Steven Parker
Steven Parker
218,692 Points

It looks like you're using the wrong function, and it's complaining that the arguments aren't correct. The strptime function expects a date and format to parse and returns a datetime, but that's not what you want here anyway.

You want to create a timedelta. You did that in the previous challenge, right? The difference here is that it's not just going to be hours this time.

Steven Parker
Steven Parker
218,692 Points

Subtracting datetimes is not the only way to get a timedelta. You can also use the timedelta constructor directly.

Nicole A
Nicole A
1,790 Points

In the previous challenge, I created a timedelta using the operation "datetime2 - datetime1". I only have one datetime here and my understanding is that the timedelta is supposed to be whatever argument is passed into the time_machine function. The problem is that I can't use the + operand to get the new datetime.