2014-02-23 45 views
0

我目前正在使用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和%時,似乎有一段時間。

任何澄清將不勝感激。

回答

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 

它的核心是圍繞在Python 2中的語句,並在Python 3

+0

那麼接下來需要什麼功能print之間的差別中心做糾正它嗎?我嘗試輸入'>>> from datetime import datetime >>> now = datetime.now() >>> >>> print('%s /%s /%s')%(now.year,now .month,now.day) %s /%s /%s',它仍然給我一個錯誤。 –

+0

@ user3342332 - 查看我編輯的python 3打印語法,或查看此鏈接:http://docs.python.org/3.1/library/string.html#format-string-syntax –

+0

謝謝,我明白了現在! –