[Tutor] Add elements to list and display it [Very newbie question]
spir
denis.spir at free.fr
Fri Feb 27 18:44:11 CET 2009
Le Fri, 27 Feb 2009 11:19:50 -0600,
Network Administrator <administrador.de.red at gmail.com> s'exprima ainsi:
> 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.
Hello Will,
Just watch the algorithm you designed for the task -- pretty sure you will find the issue yourself:
-0- init a variable that will store individual input
=== enter a loop -- will be quit on "flag" input
-1- set input list empty
-2- get user input
-3- append it to the list
-4- print list
An additional question: should 't2' be stored into the list?
denis
------
la vita e estrany
More information about the Tutor
mailing list