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

Mar Bocatcat
Mar Bocatcat
7,405 Points

Stuck at datetime.datetime

Would someone be able to help me understand how to answer this question? I think im suppose to use strs/strp to convert the string to a time. But i am not sure how/what method to use.

Any help is appreciated! Thanks!

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(ints, strs):
  strs 
  time = starter + datetime.timedelta(=+ints)
  return time

1 Answer

Martin Cornejo Saavedra
Martin Cornejo Saavedra
18,132 Points

You have to create a time delta depending on the users input in the string. The string is going to contain the format of the int. Here's my solution, let me know If this helped you.

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_int, my_string):
    if my_string == "minutes":
        my_delta = datetime.timedelta(minutes = my_int)
    elif my_string == "days":
        my_delta = datetime.timedelta(days = my_int)
    elif my_string == "hours":
        my_delta = datetime.timedelta(hours = my_int)
    elif my_string == "years":
        my_delta = datetime.timedelta(days = my_int * 365)

    return starter + my_delta
Mar Bocatcat
Mar Bocatcat
7,405 Points

Thank you this helped me!