[Tutor] User selection as both string and int

Marc Gartler gartler at express.cites.uiuc.edu
Wed Dec 15 04:13:00 CET 2004

I am trying to have a user select from amongst a list of items, and 
then make use of that choice later on as both a string (e.g. "you chose 
_____").  My function currently allows for a numerical choice, but I am 
not sure how to return it as the item (e.g. apple) rather than the 
integer corresponding to the position in the list:

def get_material(mylist):
	mycount = 1
	for item in mylist:
		print mycount, item
		mycount = mycount + 1
	material = raw_input("Please select material:")
	myposition = int(material) - 1
	return myposition

fruit_list = ['apple', 'orange', 'banana']

fruit = get_material(fruit_list)

More information about the Tutor mailing list