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

Raymond Clark
Raymond Clark
2,497 Points

"time_machine" code challenge works in my VS Code but not in challenge

Why isn't my code being acccepted in the challenge workspace but will in VS Code?

time_machine.py
import datetime

def time_machine(num, metric):
    starter_loc = starter
    amount_of_time = datetime.timedelta()
    if metric == 'days':
        amount_of_time = datetime.timedelta(days=num)
    elif metric == 'hours':
        amount_of_time = datetime.timedelta(hours=num)
    elif metric == 'minutes':
        amount_of_time = datetime.timedelta(minutes=num)
    elif metric == 'seconds':
        amount_of_time = datetime.timedelta(seconds=num)
    return amount_of_time + starter_loc

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

Steven Parker
Steven Parker
231,248 Points

The problem with testing in an external REPL is that it has no idea of what the challenge objective is, so it's up to you to perform a comprehensive test which can be more difficult than the challenge itself.

In this case, the challenge instructions say that the function 'takes an integer and a string of "minutes", "hours", "days", or "years"', but the code shown here does not accommodate the case where the argument is "years".