# ------------------------ Functions ------------------------ # def int_check(question, low=None, high=None, exit_code=None): """Check that input is an integer within bounds; return 'exit_code' if entered.""" if low is None and high is None: error = "Please enter an integer." elif low is not None and high is None: error = f"Please enter an integer that is more than or equal to {low}." else: error = f"Please enter an integer between {low} and {high} (inclusive)." while True: response = input(question).lower() if response == exit_code: return response try: response = int(response) if low is not None and response < low: print(error) elif high is not None and response > high: print(error) else: return response except ValueError: print(error) # ------------------------ Main Routine (Sample Test Area) ------------------------ # # Example use case: simulate a user guessing answers to basic math questions guess = "" while guess != "xxx": guess = int_check("Enter your answer (0-10) or 'xxx' to quit: ", low=0, high=10, exit_code="xxx") if guess != "xxx": print(f"You answered: {guess}\n") else: print("Exiting the quiz.\n")