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

Network Administrator administrador.de.red at gmail.com
Mon Mar 2 23:36:18 CET 2009


Thanks, Eric for your help!

I appreciate your explanation about the reserved word "list" as well as the
code you gently wrote to me. Now, I want to show everybody what I did:

#!/usr/bin/env python
#####################
# This function fills any given list
#
mylist = []
x = 0
while (x != 't2'):
    x = raw_input('Enter IP: ')
    mylist.append(x)
mylist.pop()

""" # Discontinued
for i in mylist:
    print i
"""

# More useful to solve my particular problem.

for i, v in enumerate(mylist):
    print i, v



It works really fine to me.

Thanks! I continue learning and making.



Will.


On Sat, Feb 28, 2009 at 1:36 AM, Eric Dorsey <dorseye at gmail.com> wrote:

> 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/20090302/a57bc50e/attachment.htm>


More information about the Tutor mailing list