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

Chigozie Beluolisa
Chigozie Beluolisa
16,263 Points

Hello dev, I am faced with a challenge trying to understand this question

Write a function named time_machine that takes an integer and a string of "minutes", "hours", "days", or "years". Return a datetime that is that far away from the starter datetime.

This was my solution:

import datetime

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

# Remember, you can't set "years" on a timedelta!
def time_machine(integer, string):
  if string == 'hours':
    Fartime = starter + datetime.timedelta(hours=integer)
  elif string == 'days':
     Fartime = starter + datetime.timedelta(days=integer)
  elif string == 'minutes':
    Fartime = starter + datetime.timedelta(minutes = integer)
  elif string == 'years':
    Modtime = integer + starter.year
    Fartime = starter.replace(year = Modtime)
  return Fartime

I have tried this but i get a bummer error that says time_machine didnt return the right answer. I have debugged my script with eclipse and everything looks fine. Any ideas ? Please ... :)

4 Answers

i dont like this teaching method .. we are beginers and the way keneth is maing an app here ... dont find it helpful

Thanks i been confused with this whole course as he keep popping things up without even explaining it

I am really glad I'm not the only one struggling with his teaching style as a beginner.

Hi Chigozie, I wasn't able to get your code to work using the Fartime variable. I kept getting an error that said the local variable referenced before assignment.

So, after correcting the code to calculate the year as Jason Anello indicated, I simply returned a value in each conditional statement, like so.

import datetime

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

def time_machine(integer, string1):
  if string1 == 'hours':
      return starter + datetime.timedelta(hours=integer)
  elif string1 == 'days':
      return starter + datetime.timedelta(days=integer)
  elif string1 == 'minutes':
      return starter + datetime.timedelta(minutes=integer)
  elif string1 == 'years':
      return starter + datetime.timedelta(days=integer*365)

# Remember, you can't set "years" on a timedelta!

time_machine(3, 'days')

Not sure if you are still stuck, but if you are, maybe this will help.

Matthew Batman
Matthew Batman
30,187 Points

I know this thread is old, but I found it on Google, so I thought I'd leave this here for anyone else in a similar situation.

Check out Chris Freeman's answer here for a shorter solution.

Instead of writing out if/elif logic for every single possible string, you can use Chris Freeman's suggestion to directly use your method variables in datetime.timedelta. You do still need at least one if to handle for 'years'.

Hi Chigozie,

I think that this challenge wants you to treat 1 year as 365 days.

You should handle the "years" case the same as the others, except multiply integer by 365 and pass it in as "days".

starter + datetime.timedelta(days=integer*365)