Is it really necessary to create a now variable to get hour, day, month etc.?


#1

https://www.codecademy.com/courses/learn-python/lessons/date-and-time/exercises/pretty-time?action=lesson_resume

It seems it is not really necessary to use a variable now. Without using such a variable, it still works. Here is my code. Is there anyone who could explain why it says us to create such a variable.

from datetime import datetime

print '%s:%s:%s' % (now.hour, now.minute, now.second)

#2

it throws an error:

maybe your browser is sending cached data, which might cause it to look like its working without the now variable


#3

very probable.
any tips for not having this situation again?


#4

having an unnamed variable (now), should raise red flags that something is off. refresh the page, run the code locally on your machine or sites like repl for verification


#5

you need to otherwise it is considered undefined. But good question. :rofl:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.