I am currently in the process of getting my feet wet with Python using Codecademy. I am learning pretty quickly and have installed and am using Pyscripter to make my own programs along side Codecademy lessons. More often than not when I copy and paste the code from Codecademy to Pyscripter and try and run it I get an error when it runs perfectly on Codecademys website. Is there a different version of Python or something? Or is codecademy not teaching the proper fundamentals? I have included a code sample and the error I receive with it.
def power(base, exponent): # Add your parameters here!
result = base**exponent
print "%d to the power of %d is %d." % (base, exponent, result)
power(37, 4) # Add your arguments here!
Error received from Pyscripter: Message File Name Line Position
SyntaxError
invalid syntax (, line 13) 13 40
Another example:
from datetime import datetime
now = datetime.now()
print ('%s/%s/%s') % (now.year, now.month, now.day)
Error: Message File Name Line Position
Traceback
21
TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
It seems to have a heck of a time when I use the %s and %.
Any clarification would be greatly appreciated.
This is the difference between Python 2 and 3, yes.
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> now = datetime.now()
>>>
>>> print ('%s/%s/%s') % (now.year, now.month, now.day)
2014/2/23
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> now = datetime.now()
>>>
>>> print ('%s/%s/%s') % (now.year, now.month, now.day)
%s/%s/%s
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
>>> print ('{0}/{1}/{2}'.format(now.year, now.month, now.day))
2014/2/23
The core of it centers around the difference between print
being a statement in Python 2 and a function in Python 3.