[Tutor] Entering a list of numbers

doubletwist@spack.nu doubletwist@spack.nu
Tue, 6 Jun 2000 07:21:21 -0700 (PDT)


Thanks for the info [and everyone else too]. I'm still getting the hang of
there being dozens of ways to do the same thing... :)



On Tue, 6 Jun 2000, Remco Gerlich wrote:

> 
> input() should only be used to enter any Python expression, like in
> the interpreter. It doesn't really have a place in normal programs, imo.
> People can enter any expression - including commands that wipe all your
> files, and so on...
> 
> Use raw_input() to get a string, then get the info you want from that.
> 
> def rollagain()
>    print "Which dice would you like to re-roll?"
>    entered = raw_input("Separate with commas. Enter 99 to keep all dice.")
>    
>    import string
>    # Split on comma - "3,4,5" results in ["3","4","5"]
>    elist = string.split(entered, ",")
>    # Turn all elements into integers
>    elist = map(int, elist)
>       
>    return elist
>    
>    
> Something like that will work better.
> 
> Btw, if you want to turn a tuple into a list, just use elist = list(entered).
> 
> And even if you did want a loop for it,
> 
> for e in entered:
>    elist.append(e)
>    
> Is a bit more Pythonic than using a count and a while loop :-)
> 
> -- 
> Remco Gerlich,  scarblac@pino.selwerd.nl
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://www.python.org/mailman/listinfo/tutor
>