[Tutor] Lists/raw_input

Andre' Walker-Loud walksloud at gmail.com
Tue Feb 7 07:03:59 CET 2012


Hi Michael,

I bet there is a better way (which I would like to see), but here is what I have come up with for my own uses.

"""
ints = []
u_in = False
while u_in == False:
	try:
		u_input = raw_input('please enter 5 integers, space separated\n    ')
		for n in u_input.split():
			ints.append(int(n))
		u_in = True
	except:
		print('    input error, try again')
"""

The while loop is set up in case your user inputs a float or string instead of just integers.  You can also imagine putting checks in case you want exactly 5 integers, etc.


Cheers,

Andre




On Feb 6, 2012, at 9:40 PM, Michael Lewis wrote:

> I want to prompt the user only once to enter 5 numbers. I then want to create a list out of those five numbers. How can I do that?
> 
> I know how to do it if I prompt the user 5 different times, but I only want to prompt the user once.
> 
> Thanks.
> 
> -- 
> Michael 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list