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.
Nicole A1,790 Points
Could someone please tell me how to write the code such that the arguments entered in the time_machine function can be used in the timedelta e.g if string, integer == days, 5, the timedelta should be days = 5.
import datetime starter = datetime.datetime(2015, 10, 21, 16, 29) def time_machine(string,integer): from datetime import timedelta return starter + timedelta() # 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)
Steven Parker221,322 Points
If you check the Python online documentation for timedelta Objects, you'll see that you can construct one by passing an argument indicating the offset. So for your example (and without any additional imports):
datetime.timedelta(days=5). Since your method will receive both the unit name and quantity as arguments, you can use those parameters to
select or build the correct statement to use.
The only other trick is that your method needs to handle "years", but since that's not one of the standard offset units some conversion will be required.