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

Khaleel Yusuf
Khaleel Yusuf
15,208 Points

Write a function named time_machine that takes an integer and a string of "minutes", "hours", "days", or "years". This d

I need so much help.

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)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Happy to help to walk you through the process!

The first step is to define the function signature. This is the line that declares the start of a function with the def keyword, followed by the desired name of the function. This function name is followed by parentheses with a list of expected parameters, if needed.

What would the function signature need to be for a function named time_machine that takes two arguments: an integer and a string?

(Leave your code as a comment below, and I'll ask you the next step by editing this post!)

Khaleel Yusuf
Khaleel Yusuf
15,208 Points
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.
def time_machine(integer, string):
## Example
# time_machine(5, "minutes") => datetime(2015, 10, 21, 16, 34)
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Great! So you have a received an argument on the amount of change in integer and the units of the change in string. The next step is to define the amount of time change timedelta() based on these two parameters. For example if the string is "minutes" you could use:

    change = datetime.timedelta(minutes=integer)

But since you don't know in advance which unit type was passed in, how do you know which argument to pass to timedelta? The simple approach is to use a series of if ... elif ... else statements to compare the value of string to "minutes", "hours", "days", etc.

In the case of "years", there isn't a timedelta parameter for 'years' so this section of code will have to convert the integer value to a usable value, such as "days", then use the "days" parameter in timedelta

Can you create the if ... elif ... else statement to set a value to "change" based on integer and string?