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

Keyword must be a string

Why won't this pass? I get keyword must be a string. If i use str(num) I get a different error message.

TIA

def time_machine(unit, num):                                                                                                                                            

    if unit == 'years':                                                                                                                                                 
        key = {'days':(num*365)}                                                                                                                                        


    else:                                                                                                                                                               
        key = {unit:num}                                                                                                                                                

    future = datetime.timedelta(**key)                                                                                                                                  
    return (starter + future)

I did this in my Python shell:

>>> unit = 'days'
>>> num = 5
>>> key = {unit: num}
>>> datetime.timedelta(**key)
datetime.timedelta(5)
>>> 

1 Answer

Hi Gabriel,

The code challenges are very specific in their wording. As you have it now, the arguments in your function definition are in the reverse order.

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

Whatever you decide to name them, the first value passed in is an integer and the second is a string. So in your code, "num" is actually the string.

Also for future posts it would be helpful if you link to the challenge you are working on.

Good luck.