-2
x = []
with open(filechoice) as fileobj:
    for word in fileobj:  
       for ch in word:

           f = ord(ch)
           x = x.append(ord(ch))

But it returns this error:

"AttributeError: 'NoneType' object has no attribute 'append'"

How can I fix this error?

ZdaR
  • 22,343
  • 7
  • 66
  • 87
Need help
  • 1
  • 1
  • 1

1 Answers1

3

The list.append() method returns None, and you replaced the list stored in x with that return value:

x = x.append(ord(ch))

Don't assign back to x here; list.append() alters the list in place:

with open(filechoice) as fileobj:
    for word in fileobj:  
       for ch in word:
           x.append(ord(ch))

You can use a list comprehension to build the list instead:

with open(filechoice) as fileobj:
    x = [ord(ch) for word in fileobj for ch in word]
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    can't you let someone else answer a python question some times :-| – David Greydanus Apr 29 '15 at 21:06
  • 1
    Yeah I agree with @DavidGreydanus, you must ignore easy questions. ;) – ZdaR Apr 29 '15 at 21:08
  • @DavidGreydanus: someone [created activity profiles](https://meta.stackoverflow.com/a/290419) for the election; I don't answer Python questions [when the circles are small](https://raw.githubusercontent.com/JC3/SO2015ActivityProfiles/master/data/combined/plots-daily/activity-35417-Martijn%20Pieters.csv-bubble-nosmoothing.png). *Usually*. :-) – Martijn Pieters Apr 29 '15 at 21:09