def get_response(self, data={}): # Parse the options and shuffle them, for variety. options = self.choices random.shuffle(options) # Loop until the user selects the correct answer. while True: # Begin by printing the menu with numerical identifiers. for i, option in enumerate(options): colors.print_option("%d: %s" % (i+1, option)) # Get the user's choice and try to return the relevant # answer, catching failures and restarting as appropriate. # Note that invalid input restarts the loop here, but an # incorrect answer will restart the loop outside of the # scope of the function. This allows for grading. # XXX: Try to match strings to choices, too. try: colors.print_inst("Select one of the numbered choices: ", end="") choice = int(input())-1 return options[choice] except (ValueError, IndexError): colors.print_help( "Please pick an integer between 1 and %d" % len(options)) continue
def get_response(self, data={}): # Parse the options and shuffle them, for variety. options = self.choices random.shuffle(options) # Loop until the user selects the correct answer. while True: # Begin by printing the menu with numerical identifiers. for i, option in enumerate(options): colors.print_option("%d: %s" % (i + 1, option)) # Get the user's choice and try to return the relevant # answer, catching failures and restarting as appropriate. # Note that invalid input restarts the loop here, but an # incorrect answer will restart the loop outside of the # scope of the function. This allows for grading. # XXX: Try to match strings to choices, too. try: colors.print_inst("Select one of the numbered choices: ", end="") choice = int(input()) - 1 return options[choice] except (ValueError, IndexError): colors.print_help("Please pick an integer between 1 and %d" % len(options)) continue
def execute(self): """Repeatedly prompts the user for lessons to run until explictly exited.""" # Loop until user EOF. while True: # Present the menu. self.menu() try: colors.print_inst("Selection: ", end="") identifier = input().strip() self.execute_lesson(identifier) except NoSuchLessonException: colors.print_err("No lesson: %s" % identifier) except EOFError: # If the user hits CTRL-D, exit. # XXX: Tell the user this. print() print("Bye!") break