PDA

View Full Version : Python Problem. Adding to lists.




larswik
Mar 16, 2009, 05:53 PM
Hello, working on a simple program but I am at a loss since I am not geting the results that I thought I would get. I am trying to get the user to enter a name and have that name added to the list. But instead of adding the name to the list it is creating a new element for each letter in the name instead of the name it's self. the results look like this printed out.

['mike', 'tom', 'bob', 'l', 'a', 'r', 's']

Here is the code...

name = ["mike", "tom", "bob"]
print name

name_entree = raw_input("Please enter a name: ")
person = name_entree
name += person

print
print name

# I know I covered this in the book but for the life of me I can't seem to find it. Any clarification would be great.

-Lars



Cromulent
Mar 16, 2009, 06:16 PM
First of all there is no need for the person variable in your code.

Second, the code should read:

name_entree = raw_input("Please enter a name: ")
name.append(name_entree)

larswik
Mar 16, 2009, 08:34 PM
Ahhh, that is right the '.append'

Thanks!

-Lars