-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'>
2
  • names modifies the list, it doesn't return the modified list. Commented Jul 12, 2020 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. Commented Jul 12, 2020 at 21:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.