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

Jordan Bester
Jordan Bester
4,752 Points

Converting years into days using python?

Im not understanding This section too well and this question has me stumped?? any suggestions??

time_machine.py
import datetime
from datetime import timedelta

starter = datetime.datetime(2015, 10, 21, 16, 29)

def time_machine(minutes=32, hours=23, days=30, weeks=52):
  return datetime = time_machine - 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
Steven Parker
229,732 Points

If you have the rest of this task down, the part about years is simple. When the units are specified in "years', just handle it the same way as you do "days", but multiply the value by 365.

If you need some hints on the task itself:

  • you will need to test the string given for units for each of the four possible choices
  • in each case, you will construct a timedelta using the numeric value for those units
  • then you will adjust starter using your timedelta
  • finally you will return the adjusted datetime

I'm betting you can get it now without an explicit spoiler.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

bonus hint: to use a variable as a keyword argument to timedelta use the ** syntax:

timedelta(**{units: value))

Jordan Bester
Jordan Bester
4,752 Points

Guys I have no Idea what I am doing here. very confused on how to set up the units? and convert the years into days! Help!