-2

I'm trying to create a list of size n whose elements are names - both are user inputs. Here is the following code:

names = []
for _ in range(int(input("Number of names to query (n): "))):
    names = names.append(input("Enter the names to query: "))

I get an AttributeError: 'NoneType' object has no attribute 'append' I don't understand what this means and why this is happening. names seems to be a list object?

print(type(names))
<class 'list'>
  • `names` modifies the list, it doesn't return the modified list. – Barmar Jul 12 '20 at 21:15
  • The return value of `names.append` is `None`, so when you reassign that to `names` you have lost your list. Don't reassign the variable `names`; `list.append` modifies in-place. – nog642 Jul 12 '20 at 21:16

1 Answers1

0

.append will edit the existing list. You don't need to assign it to a variable.

These types of methods in python often return None. So you are setting names to None and then trying to call .append on it again.

fooiey
  • 1,040
  • 10
  • 23