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