Code works in lesson but exact code pasted to compiler does not


#1

Link to lesson:
https://www.codecademy.com/courses/python-beginner-en-zFPOx/0/4?curriculum_id=4f89dab3d788890003000096

I am not sure why this is happening or if it matters in the end. This bit of code works in the lesson and allows me to move forward:

from datetime import datetime
now = datetime.now()

print '%s/%s/%s' % (now.month, now.day, now.year)

However that same bit of code gives an error in the shell as well as PyCharm:

**print '%s/%s/%s' % (now.month, now.day, now.year)**

** ^**
SyntaxError: invalid syntax

Process finished with exit code 1

It works fine if I print each one in a line by itself but not when I use %. As I do the little projects after lesson chunks I also work on them in PyCharm. This is the first time where this has happened to me. I have looked at this for a while, is there something I am missing?

Thank you very much for your help.


#2

This is Python 2.*. Python 3.* requires parenthesis around the expression, and has a new .format() function for string formatting. Try adding parentheses, and see if it lets the old string formatting still play out.

print ( '...' )

#3

@dedrabbit,
Try it by using

print("{0}/{1}/{2}".format(now.month, now.day, now.year))

And with a google search
== discussions / opinions ==
python pycharm print statement site:stackoverflow.com
found
http://stackoverflow.com/questions/22738455/pycharm-unresolved-reference-print
OR
python print statement vs print function site:stackoverflow.com
http://stackoverflow.com/questions/6239887/what-is-the-advantage-of-the-new-print-function-in-python-3-x-over-the-python-2
http://stackoverflow.com/questions/26938799/printing-variables-in-python-3-4


#4

Thanks... sorry for the dumb question. What is even more dumb is I have been doing these lessons thinking I have been on Python 3 haha...

Thank you, I will try to do better research before posting again.