[Tutor] Inputting elements of a list
Alan Gauld
alan.gauld at btinternet.com
Mon Jan 22 10:35:01 CET 2007
"vanam" <vgvr620034 at gmail.com> wrote
> For standard input from the keyboard raw_input will be used for
> string and
> input for number.
input can read more than numbers. It will take whatever you type
and try to evaluate it as a Python expression. That's why input is
so dangerous and should be avoided in programs you are distributing
since a malicious user could enter a command that destroys your
data (or worse!).
But for your own use you can do things like:
>>> L = input('Enter a list: ')
Enter a list: [1,2,3,4,'five']
>>> print L
[1, 2, 3, 4, 'five']
>>>
In real programs its better to use raw_input and
convert the string to the expected value, hence to read a number:
n = int( raw_input('enter a number: ') )
to read a list you need to split the input string by some agreed
separator, say commas:
L = raw_input('enter a comma separated list: ')
L = L.split(',')
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list