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 trialAf Mondal
2,437 Pointstime_machine is not returning the correct datetime
I don't know what am I doing wrong. Please help....
import datetime
from datetime import timedelta
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, string):
return starter + timedelta('string' = num)
1 Answer
Nathan Tallack
22,160 PointsOk. A few things.
First, you can call timedelta as datetime.timedelta with that first import negating the need for the second import. Good practice that way. :)
Second, you need to avoid using string as a parameter name because that is a reserved word in Python. Also you should use something other than num, something more descriptive like unit because it is the unit of time you are evaluating.
Last, you need to put in a case for each different kind of time. I've got your minutes example in here. You'll need to put some else conditions in to cover all the other kinds (hours, days, years). But remember years is 365 days for the case of this challenge. ;)
def time_machine(num, unit):
if unit == 'minutes':
return starter + datetime.timedelta(minutes = num)
Af Mondal
2,437 PointsAf Mondal
2,437 PointsThank you Nathan for this brief explanation. Finally i figure it now.