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

Time machine problem - Where is the mistake?

Good Evening,

Can anyone spot my mistake? It says: local variable 'starter' referenced before assignment

Here is my code:

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(int1,string1):
    if string1=="minutes":
        starter=starter+datetime.timedelta(minutes=int1)
        return starter
    elif string1=="hours":
        starter=starter+datetime.timedelta(hours=int1)
        return starter
    elif string1=="days":
        starter=starter+datetime.timedelta(days=int1)
        return starter
    elif string1=="years":
        starterr=starter + datetime.timedelta(days=int1*365)
        return starter

Thanks in Advance!

1 Answer

You are trying to redifne the starter variable at line three, use another variable name on your time_machine function. Also you need to make that new variable different from starter, then return the value of starter and that new variable.

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(int1,string1):
    if string1=="minutes":
        notStarter = datetime.timedelta(minutes = int1)
    elif string1=="hours":
        notStarter = datetime.timedelta(hours = int1)
    elif string1=="days":
         notStarter = datetime.timedelta(days = int1)
    elif string1=="years":
        notStarter = datetime.timedelta(days = int1*365)


    return starter + notStarter

Thanks!