[Tutor] how to take user input to form a list?

Alan Gauld alan.gauld at freenet.co.uk
Sun Nov 14 09:08:42 CET 2004


> and i write a code like this which is not working:
> def list(let1,let2,let3,let4):
>    let1,let2,let3,let4=raw_input("enter 4 letters:")

raw_input always returns a single string. You need 
to separate, or parse, out the individual components.
So:

def getList():
  s = raw_input('Enter 4 comma separated letters')
  lst = s.split(',')
  return lst

Of course you would probably need a lot more 
error detection than that in reality, to ensure 
there were only 4 letters, that they were comme 
separated etc.

THe other thing to watch is that you should not 
use Python reserved words like list as variable 
names because if you do you will not be able 
to use the Python functions of the same name!

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


More information about the Tutor mailing list