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) 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
231,236 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
231,236 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.