def get_selection(l, q = ""): selection = None while selection is None: if q: print q for i, obj in enumerate(l): print i, obj try: selection = int(raw_input(">>>")) except ValueError: print "I'm sorry, you didn't type a valid index" else: try: l[selection] except IndexError: print "I'm sorry, you didn't type a valid index" selection = None return selection