Codecademy Python和Pyscripter给出错误消息
问题描述:
我目前正在使用Codecademy让我的脚湿透Python。我学习速度非常快,已经安装并正在使用Pyscripter在Codecademy课程中创建自己的程序。当我将Codecademy的代码复制并粘贴到Pyscripter并尝试运行它时,通常会在Codecademys网站上完美运行时发生错误。是否有不同的Python版本?或者是Codecademy没有教导适当的基础?我已经包含了一个代码示例和我收到的错误。从Pyscripter接收Codecademy Python和Pyscripter给出错误消息
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!
错误:消息文件名线位置
的SyntaxError
无效的语法(13行)13 40
又如:
from datetime import datetime
now = datetime.now()
print ('%s/%s/%s') % (now.year, now.month, now.day)
错误:消息文件名线位置
回溯
TypeError:不支持的操作数类型为%:'NoneType'和'元组'
当我使用%s和%时,似乎有一段时间。
任何澄清将不胜感激。
答
这是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
它的核心是围绕在Python 2中的语句,并在Python 3
那么接下来需要什么功能
print
之间的差别中心做纠正它吗?我尝试输入'>>> from datetime import datetime >>> now = datetime.now() >>> >>> print('%s /%s /%s')%(now.year,now .month,now.day) %s /%s /%s',它仍然给我一个错误。 –@ user3342332 - 查看我编辑的python 3打印语法,或查看此链接:http://docs.python.org/3.1/library/string.html#format-string-syntax –
谢谢,我明白了现在! –