0

Hi am new to python found this code in one book and wanted to try it but line 4 says its an error "encountered type when expecting one of the following and a list of brackets. How to fix it?

#: 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]

#:~
7
  • Should work. What's the problem? Commented Mar 25, 2012 at 21:43
  • Works for me, are you sure this is exactly what you're trying to execute? Commented Mar 25, 2012 at 21:47
  • Are you sure this code does not work? I just copy-and-pasted and tried it out, it worked all fine. Commented Mar 25, 2012 at 21:47
  • I'm just seeing a plain syntax erro Commented Mar 25, 2012 at 21:48
  • Are you using a very (very, very) early version of Python? Commented Mar 25, 2012 at 21:52

1 Answer 1

4

You are using Python 3.x, where print is a function and no longer a statement. The book is written for Python 2.x, where print still is a statement.

You fix it by using a version of Python that matches what the book describes, or get a book for a newer version of Python (3.x).

Your immediate problem can be solved by writing

print (type(aList))
Sign up to request clarification or add additional context in comments.

1 Comment

Only thing that I can see, too. Python 2.x interpreters are A-OK with the code as presented.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.