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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Aizah Sadiq](https://secure.gravatar.com/avatar/5423495ce21caa1b2900d7b01dc524c0?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Aizah Sadiq
2,434 PointsThis is confusing
How should I go about this challenge
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)
4 Answers
![Jeff Muday](https://uploads.teamtreehouse.com/production/profile-photos/5900142/micro_jeff_face.jpg)
Jeff Muday
Treehouse Moderator 28,718 PointsA typo crept into your code with a parenthesis... see solution below.
The variable delta
is the generic timedelta. Also, years
is not a parameter of timedelta
so we have to use the trick of converting years into days.
days = x * 365
change hourdelta
to delta
change daydelta
to delta
change yeardelta
to delta
This solution below will work
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(x, time_unit):
# The line above is where the syntax error is
if time_unit == "minutes":
# this is how to compute a time "delta" for minutes
delta = datetime.timedelta(minutes=x)
if time_unit == "hours":
delta = datetime.timedelta(hours=x)
if time_unit == "days":
delta = datetime.timedelta(days=x)
if time_unit == "years":
# here is the tricky part, timedelta doesn't support years parameter
# so we convert it to days (e.g. 365 days in a year)
x = x * 365
delta = datetime.timedelta(days=x)
# return the value of the starting date and a delta
return starter + delta
![Rocket Dollar Invest](https://uploads.teamtreehouse.com/production/profile-photos/10750982/micro_rd-logo-fullColor.jpg)
Rocket Dollar Invest
7,129 PointsA simple solution for this problem is to first check if the string is 'years' and if so change the string to 'days' since timedelta will not except years as an argument. Then multiply the integer by 365 to get the equivalent number of days for the given number of years. Finally, return the duration as the difference of the starter datetime and the timedelta of the string and integer passed to the method as a literal dictionary with the ** prefix operator to unpack the dictionary.
import datetime
starter = datetime.datetime(2015, 10, 21, 16, 29)
def time_machine(integer, string):
if string == 'years':
string = 'days'
integer *= 365
return starter + datetime.timedelta(**{string: integer})
![Jeff Muday](https://uploads.teamtreehouse.com/production/profile-photos/5900142/micro_jeff_face.jpg)
Jeff Muday
Treehouse Moderator 28,718 PointsThis is an interesting challenge-- they want you to create a function that returns a NEW Python datetime type based on a number and a string which represents a unit of time (e.g. minutes, hours, days, years).
You should refer to the documentation on datetime to learn more:
https://docs.python.org/3/library/datetime.html
There are a number of different ways to do this. There are some really compact solutions, but I am trying to keep it simple.
BELOW IS NOT A COMPLETE SOLUTION, BUT A SUGGESTION
def time_machine(x, time_unit):
if time_unit == "minutes":
# this is how to compute a time "delta" for minutes
delta = datetime.timedelta(minutes=x)
if time_unit == "hours":
# you fill in the code to make this work
pass
if time_unit == "days":
# you fill in the code to make this work
pass
if time_unit == "years":
# this is the tricky one, since years must be converted to days
pass
# return the value of the starting date and a delta
return starter + delta
![Aizah Sadiq](https://secure.gravatar.com/avatar/5423495ce21caa1b2900d7b01dc524c0?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Aizah Sadiq
2,434 PointsHi Jeff! Thank you for your assistance but for some reason, I am given a syntax error.
import datetime
starter = datetime.datetime(2015, 10, 21, 16, 29
def time_machine(x, time_unit):
# The line above is where the syntax error is
if time_unit == "minutes":
# this is how to compute a time "delta" for minutes
delta = datetime.timedelta(minutes=x)
if time_unit == "hours":
hourdelta = datetime.timedelta(hours=x)
if time_unit == "days":
daydelta = datetime.timedelta(days=x)
if time_unit == "years":
years = days * 365
yeardelta = datetime.timedelta(years=x)
# return the value of the starting date and a delta
return starter + delta
# 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)
I also feel a little lost in the recent challenges. So any guidance would be greatly appreciated.