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

not sure why this is not working

cant see anything wrong. please 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)

def time_machine(integer, string):
    integer = int(integer)
    string = str(string)
    if string == 'years':
        integer = integer * 365
    td = datetime.timedelta(string = integer)
    return (starter + td)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Hey Em Noble, there are two errors happening in the code.

First, using (string=integer) will not do what you’re expecting. The string will be interpreted as the name of a keyword argument and not be evaluated as a variable. This is why you may see the error ’string’ not a valid keyword argument for this function.

One long solution is to use a series of if/elif/else statements to call timedelta with different keywords depending on the value of string. Another method is to create a dictionary and use dictionary unpacking to get the correct keyword.

timedelta(**{string=integer})

This works because string will be evaluated as a variable in the creation of the dictionary and will then have the correct value when interpreted as an argument keyword!

The second error is not resetting string = days in the if block.

Post back if you need more help. Good luck!!!