2012-03-25 22 views
0

こんにちは、このコードを1つの本で見つけて試したかったのですが、4行目でエラーが発生しました。 "次のいずれかと括弧のリストを期待しているときにタイプが見つかりました。 printはもはや声明の関数であると。この本は、印刷がまだの文であるのPython 2.xでは、のために書かれているあなたは、Pythonの3.xを使用している型宣言pythonエラー

#: arrays/PythonLists.py 

aList = [1, 2, 3, 4, 5] 
print type(aList) # <type 'list'> 
print aList # [1, 2, 3, 4, 5] 
print aList[4] # 5 Basic list indexing 
aList.append(6) # lists can be resized 
aList += [7, 8] # Add a list to a list 
print aList # [1, 2, 3, 4, 5, 6, 7, 8] 
aSlice = aList[2:4] 
print aSlice # [3, 4] 


class MyList(list): # Inherit from list 
    # Define a method, 'this' pointer is explicit: 
    def getReversed(self): 
     reversed = self[:] # Copy list using slices 
     reversed.reverse() # Built-in list method 
     return reversed 

list2 = MyList(aList) # No 'new' needed for object creation 
print type(list2) # <class '__main__.MyList'> 
print list2.getReversed() # [8, 7, 6, 5, 4, 3, 2, 1] 

#:~ 
+0

を書き込むことによって解決することができます。どうしたの? –

+0

私のために働く、これは正確にあなたが実行しようとしているものですか? –

+0

このコードはうまくいきませんか?私はちょうどコピー&ペーストして試してみました。 – gefei

答えて

4

、。

あなたはそれを修正します本の説明と一致するPythonのバージョンを使用するか、新しいバージョンのPython(3.x)の本を入手してください。

01動作するはず

あなたの当面の問題は

print (type(aList)) 
+0

私には分かります。 Python 2.xインタプリタはA-OKで、コードは表示されています。 – Makoto