Why did Codecademy Code not work on Python?


So on lesson 2/6 the datetime lesson where it teaches you how to make Python tell you your current time and date. I decided to try the code on Python, since it worked in Codecademy I decided to use the same exact code. It didn’t work. After 2 more tries I realized I had to put a parentheses around “now” after “print”. My question is why?, also Why did it work on Codecademy put not Python?


from datetime import datetime
now = datetime.now()
print now

<do not remove the three backticks above>

>>> from datetime import datetime
>>> now = datetime.now()
>>> print (now)
2017-11-12 11:36:39.041156

If you are using Python 3, your print statement as posted will raise a syntax error.


I know but why? Why is the code possible on Codecademy?


Because CC uses Python 2, not 3. In version 2, print is a construct; whereas in version 3 it is a function so MUST be written as a function call with parens.


What is a construct, can you give me a link to explain it. Thank you!


A language construct is a syntactically allowable part of a program that may be formed from one or more lexical tokens in accordance with the rules of a programming language.



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