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 trialmatthew mahoney
Python Development Techdegree Student 2,536 PointsWrite a function named time_machine that takes an integer and a string of "minutes", "hours", "days", or "years". This d
Hey there, Struggling with datetimes in general. It Will def be a subject I come back to again. Can't figure out how to calculate "years"... please help.
Thanks, Matt
import datetime
starter = datetime.datetime(2015, 10, 21, 16, 29)
# 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)
def time_machine(take_int, take_string):
if take_string == 'minutes':
offset = datetime.timedelta(minutes=take_int)
elif take_string == 'hours':
offset = datetime.timedelta(hours=take_int)
elif take_string == 'days':
offset = datetime.timedelta(days=take_int)
else:
offset = datetime.timedelta(days,365=take_int)
return starter + offset
3 Answers
Jonathan Grieve
Treehouse Moderator 91,253 PointsTry passing in a little calculation to the else block. All you need to do is multiply the timedelta
day by the integer 365
Preston Dunn
Python Development Techdegree Student 3,723 PointsStruggled heavily matthew mahoney, I am also going to have to review these videos a few times
nicolea
5,048 PointsHere's my response:
def time_machine(take_int, take_string): if take_string == 'minutes': offset = datetime.timedelta(minutes=take_int) elif take_string == 'hours': offset = datetime.timedelta(hours=take_int) elif take_string == 'days': offset = datetime.timedelta(days=take_int) else: offset = datetime.timedelta(days=take_int*365) return starter + offset
matthew mahoney
Python Development Techdegree Student 2,536 Pointsmatthew mahoney
Python Development Techdegree Student 2,536 PointsJonathan Grieve would that be before the "offset = datetime.timedelta(days,365=take_int) part... sadly I'm still not getting it
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsI'll give you a little bit more of a nudge. :)
It's just like what you did before in the last
else if
except in this block, if none of the other conditins match you're getting the day timedelta and simply multiplying that value by 365.matthew mahoney
Python Development Techdegree Student 2,536 Pointsmatthew mahoney
Python Development Techdegree Student 2,536 PointsJonathan Grieve thanks! I'm going to be watching and rewatching the DateTime videos... for some reason, it's a concept that isn't sticking. Has anyone else struggled with this set of videos by Kenneth Love?