0

私は現在、Codecademy を使用して Python に慣れようとしています。私はかなり早く学習しており、Pyscripter をインストールして使用し、Codecademy のレッスンと並行して独自のプログラムを作成しています。Codecademy から Pyscripter にコードをコピー アンド ペーストして実行しようとすると、Codecademy の Web サイトで完全に実行されたときにエラーが発生することがよくあります。別のバージョンの Python などはありますか? それとも、Codecademy は適切な基礎を教えていないのでしょうか? コード サンプルと、それと共に受け取ったエラーを含めました。

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)

Error: Message File Name Line Position
Traceback
21
TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
%s と % を使用すると、かなり時間がかかるようです。

明確化をいただければ幸いです。

4

1 に答える 1

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

printその核心は、Python 2 のステートメントと Python 3 の関数の違いに集中しています。

于 2014-02-23T08:01:02.117 に答える