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

Rogier Leegwater
Rogier Leegwater
5,589 Points

Write a function named time_machine that takes an integer and a string of "minutes", "hours", "days", or "years". This d

Write a function named time_machine that takes an integer and a string of "minutes", "hours", "days", or "years". This describes a timedelta. Return a datetime that is the timedelta's duration from the starter datetime.

Hi Rogier,

You'll want to make sure you're asking a specific question instead of just the Challenge question you're having difficulty with.

You'll also want to make sure you post what code you've tried so far that hasn't worked; that way, we can guide you in the right direction (so that you'll know where your code went wrong).

This link can be really helpful to understand how to post code in the forums so that it displays correctly.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

The challenge is asking you to Write a function named time_machine that takes an integer and a string as arguments. The string will have a value of "minutes", "hours", "days", or "years". [These can be used to define] a timedelta object. Return a datetime object that is the timedelta's duration from the starter datetime. That is, add the timedelta to the provide starter datetime.

In the code editor, you are given the hint

Remember, you can't set "years" on a timedelta!. Consider a year to be 365 days.

A timedelta is defined as datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) (See timedelta docs)

So the flow would be:

  • check string argument for the "units"
  • if units is "years" convert to "days" by multiplying by 365
  • create a timedelta using these units and the integer argument
  • add this timedelta to the starter datetime (which creates a new datetime)
  • return the new datetime

Good Luck! Post back if you have more specific questions