0

I need help with this code, I get an error message that says

File "<tmp 6>", line 4, in <module>
n.append(names)

AttributeError: 'str' object has no attribute 'append'

Code:

names = ['','','','','']   
for i in range(1,6):    
    n = input("Enter a name: ")   
    n.append(names)          
print (names)
2
  • 1
    You should append the value n to the list names, i. e. names.appned(n); but you did the opposite. Plus, if you want to get 5 names from user, you shouldn't initialize your list; you just define an empty list like this: names = []. Otherwise you'll have a list containing 5 empty strings and 5 other strings obtained from user. Commented Dec 28, 2015 at 13:47
  • May be more helpful tutorial: AttributeError: ‘str’ object has no attribute ‘append’ Commented Mar 8, 2022 at 4:38

2 Answers 2

5

If you're trying to append the string n to the list names, you got the syntax backwards.

names.append(n)

You should also probably indent it so it's inside the loop:

for i in range(1,6):    
    n = input("Enter a name: ")   
    names.append(n)
Sign up to request clarification or add additional context in comments.

Comments

2

You're trying to append to your input String n the list names where the string should be added. It should be the opposite.

names = ['','','','','']   
for i in range(1,6):    
    n = input("Enter a name: ")   
names.append(n)          
print (names)

1 Comment

Thanks for your answer!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.