[Tutor] Add elements to list and display it [Very newbie question]

Eric Dorsey dorseye at gmail.com
Sat Feb 28 08:36:08 CET 2009


Here is one possible implementation of your project.

*Code:*
#Dont use list as a variable name, its one of the reserved words.
mylist = []

#realize any values captured here are strings
x = raw_input('Enter num or text: ')
mylist.append(x)
x = raw_input('Enter num or text: ')
mylist.append(x)

#output the type of objects you've entered (hint: they'll always be
strings.. ;)
print type(mylist[0])
print type(mylist[1])

#print the list of items
for i in mylist:
    print i

*When you run the program:*
Enter num or text: 27
Enter num or text: Eric
<type 'str'>
<type 'str'>
27
Eric


On Fri, Feb 27, 2009 at 10:19 AM, Network Administrator <
administrador.de.red at gmail.com> wrote:

> I am beggining to learn Python and I appreciate if you help me with this:
>
> "I want a piece of a program to request the user to input "elements"
> (numbers, text, etc) and store them into a list. Then, I want to display all
> the elements one-per-line."
>
> I started using this code:
>
> #!/usr/bin/env python
> #####################
> # This function fills any given list
> # and display its content.
> #
> x = 0                           # Variable "x" initiallized to zero, just
> because Python required it
> while (x != 't2' ):         # On user's input "t2", no more input must be
> required
>     list = []                    # I start a zero-elements list
>     x = raw_input('Enter your number or text: ')                # Software
> asks for user's input.
>
> list.append(x)
> # User's input is append to the list "list"
>
> for x in list:                  # It asks to enter the list and...
>     print x                      # print their elements.
>
> Unfortunately, this code fails to do what I expect. I notice that user's
> input is not being append to the list, so, when I require to print the
> elements of the list only "t2" is displayed. I don't know how to append
> elements to a list on user's input.
>
> I appreciate your clearence.
>
> Regards,
>
>
> Will.
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090228/56957fee/attachment.htm>


More information about the Tutor mailing list