Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Dates and Times in Python 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