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 Simple Time Machine

Hervé Dauberton
Hervé Dauberton
8,163 Points

Simple time machine challenge in python

Hello,

I'm stuck in one of the challenges with the python time courses. I must write a function which will change a datetime by a certain number of hours. I did something wrong but i have no idea what. And it works properly on the workspace. What's wrong ?

Ps: English is not my mother language and i'm absolutely not sure if ahead of a time is before or after but my code doen't pass doesn't either way.

time_machine.py
import datetime

def delorean (yop):
  starter = datetime.datetime(2015, 10, 21, 16, 29)
  return starter-datetime.timedelta(hours=yop)

2 Answers

Kai Munechika
Kai Munechika
2,886 Points

Hi,

Variables that are created in a function are lost after the function returns a value, while variables created on the outside are not. Therefore, the line starter = datetime.datetime(2015, 10, 21, 16, 29) should be outside of your function, since the workspace must be able to compare the variable starter to the output of your function. And since we want to return a datetime that is many hours ahead, the last line should have the add operator, not subtract

Hervé Dauberton
Hervé Dauberton
8,163 Points

There was no need of starter outside of the function in that case, it worked out like that properly on workspace but it's interesting to learn (or relearn if i forgot it) about that kind of information about the variables. And yes, it was my mistake. So in that case ahead will mean 'after' not 'before', duly noted.

Thank you very much for your quick answer !

edit : i really don't get why my thank you is on code style