Codecademy Python和Pyscripter给出错误消息

时间:2014-02-23 07:55:17

标签: python pyscripter

我目前正在使用Codecademy使用Python弄湿我的脚。我正在快速学习并安装并使用Pyscripter在Codecademy课程中创建自己的程序。当我将代码从Codecademy复制并粘贴到Pyscripter并尝试运行它时,我会在Codecademys网站上运行时遇到错误。是否有不同版本的Python或其他东西?或者代码大会没有教导正确的基础知识吗?我已经包含了一个代码示例和我收到的错误。

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!

从Pyscripter收到错误:消息文件名称行位置
的SyntaxError
    语法无效(第13行)13 40

另一个例子:

from datetime import datetime
now = datetime.now()

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

错误:消息文件名称行位置
回溯
           21个
TypeError:%支持的操作数类型:'NoneType'和'tuple'
当我使用%s和%时,似乎有一段时间。

任何澄清将不胜感激。

1 个答案:

答案 0 :(得分:0)

这是Python 2和3之间的区别,是的。

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

它的核心在于{2}是Python 2中的语句和Python 3中的函数之间的区别。