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.
matthew mahoneyPython Development Techdegree Student 2,536 Points
Write 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.
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
Jonathan GrieveTreehouse Moderator 89,889 Points
Try passing in a little calculation to the else block. All you need to do is multiply the
timedelta day by the integer 365