[Tutor] User Input List and Sort

Rikard Bosnjakovic rikard.bosnjakovic at gmail.com
Wed Feb 14 22:23:17 CET 2007


On 2/14/07, kubota2550 at gmail.com <kubota2550 at gmail.com> wrote:

> Every example of a list that I've seen involves working with, sorting, etc
> pre-populated list.  How can I create a list where the user inputs a string
> of numbers and then sorts it?

The code of yours gets a commaseparated _string_, which you are
putting into a list. The list contains one element only; the string
you entered.

What you want to do is this:

print "Input list separated by commas"
foo = raw_input()
lst = foo.split(",")
print lst
print len(lst)
lst.sort()
print lst

> leading me to believe that it is seeing
> the input as 1 large input instead of separate numbers.

Correct.

raw_input() is not smart enough to split the elements for you, so you
need to do the dirty work. The string-method "split" does this. Type
help("".split) for more info.


-- 
- Rikard.


More information about the Tutor mailing list