Khalid
Khalid

Reputation: 13

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

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)

Upvotes: 0

Views: 10970

Answers (2)

miguels
miguels

Reputation: 636

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)

Upvotes: 2

Kevin
Kevin

Reputation: 76194

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)

Upvotes: 5

Related Questions