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 trialNicole A
1,790 Pointspython 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.
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
231,236 PointsIt 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.
Nicole A
1,790 PointsIn 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.
Steven Parker
231,236 PointsSteven Parker
231,236 PointsSubtracting datetimes is not the only way to get a timedelta. You can also use the timedelta constructor directly.