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

SUDHARSAN CHAKRAVARTHI
PLUS
SUDHARSAN CHAKRAVARTHI
Courses Plus Student 2,434 Points

Date and Time - time machine

In the function time_machine, like to know what needs to do with the first parameter "integer". Bevause all other parameters are string and parsed into datetime.

time_machine.py
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(intdata, minutes, hours, days):
  new_minute = datetime.datetime.strptime(minutes,'%M')
  new_hour = datetime.datetime.strptime(hours,'%H')
  new_day = datetime.datetime.strptime(days,'%d')

  new_time = starter + datetime.timedelta(hours=new_hour.hour, minutes = new_minute.minute, days = new_day.day)

  return new_time

2 Answers

AR Ehsan
AR Ehsan
7,912 Points
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)

time_machine(3, 'days')
SUDHARSAN CHAKRAVARTHI
SUDHARSAN CHAKRAVARTHI
Courses Plus Student 2,434 Points

Thank You very much. You have given complete code. i misunderstood the method arguments.

Paxton Butler
Paxton Butler
6,338 Points

do you have to use an if/else statement for this?

I assumed (maybe wronlgy) that you could do something like:

return starter + datetime.timedelta(string1=integer)

but I couldn't get it to work and thought it might be a formatting issue?

AR Ehsan
AR Ehsan
7,912 Points

If this was helpful, fell free to mark as best answer