[Tutor] Lists/raw_input
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@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lists/raw_input
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@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lists/raw_input
On 2012/02/07 07:40 AM, 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@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor You can take your input from your user in one line seperated by something like a space and then split it after you capture it so for eg. user_input = raw_input('enter 5 numbers seperated by a space each: ') list_from_input = user_input.split() # Split by default splits on spaces, otherwise you need to specify the delimiter # Then you can validate the list to ensure all 5 are actually numbers, otherwise prompt the user to re-enter them Hope that help. -- Christian Witts Python Developer // ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor