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 trialRonit Mankad
12,166 PointsToo complicated
Can anyone please help. I can't seem to figure this out.
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(num, st):
2 Answers
Andrew Mlamba
15,642 PointsHi Ronit,
Remember that a timedelta object represents a duration, the difference between two dates or times.
see my solution below.
You can find extensive discussions on this here
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(my_integer, my_string):
if my_string == 'minutes':
return starter + datetime.timedelta(minutes=my_integer)
elif my_string == 'hours':
return starter + datetime.timedelta(hours=my_integer)
elif my_string == 'days':
return starter + datetime.timedelta(days=my_integer)
elif my_string == 'years':
return starter + datetime.timedelta(days=my_integer * 365)
# print(time_machine(my_integer=5, my_string='hours'))
Mduduzi Frederick Dube
8,886 PointsYou are right to say complicated. All the time the challenges had needed simple reasoning, and a few lines of code. This one needs you to evaluate inputs based on the arguments of your time_machine function.
If the string arg is minutes do this.. if the string arg is days do this...
This one passed it for me, its just telling the program to calculate the neccessary time to be added using the right unit of time. Remember the timedelta() method only accepts three arguments and these are days, seconds and microseconds. This is the Link (https://docs.python.org/3/library/datetime.html?highlight=timedelta#datetime.timedelta)
I wrote it like this;
''' python
def time_machine(integer,string):
if string.upper() == 'MINUTES':
new_int = integer * 60
new_date = starter + datetime.timedelta(seconds=new_int)
return new_date
elif string.upper() == 'HOURS':
new_int = integer * 3600
new_date = starter + datetime.timedelta(seconds=new_int)
return new_date
elif string.upper() == 'DAYS':
new_int = integer
new_date = starter + datetime.timedelta(days=new_int)
return new_date
elif string.upper() == 'YEARS':
new_int = integer * 365
new_date = starter + datetime.timedelta(days=new_int)
return new_date
return new_date
'''
Steven Parker
231,248 PointsSteven Parker
231,248 PointsGive a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
― Maimonides