def repeat_input(prompt='? ', in_type=str, num_retries=-1):
    """
    Get input of a specific type, repeating on failure.

    Prompt for a specific type of input.  If the input cannot be converted to
    that type, retry the input for the specified amount of retries.  If the
    retry amount is negative, the function will retry indefinitely.  If the
    parameter is omitted, it will be supplied as -1.  If the user enters a
    blank string, this is considered a cancel sequence, and None is returned.
    Similarly, if the number of retries is exceeded, None is returned.

    :param str prompt: prompt to display each time input is requested
    :param type in_type: type or function to use to cast the input.  Should
                         raise :class:`ValueError` if input is invalid.
    :param num_retries: number of times to retry before returning.  -1 for
                        infinite.
    :returns: Input casted to ``in_type``
    """
    rv_str = input(str(prompt))  # Ask for input the first time
    rv = None
    if rv_str == '':
        return None  # Blank strings mean return None
    try:
        rv = in_type(rv_str)  # Succeeds if the input was the right type
    except ValueError:
        while rv is None and num_retries != 0:
            try:
                # Continue retrying until we get a successful conversion
                num_retries -= 1
                rv_str = input('Bad Input.  ' + str(prompt))
                if rv_str == '':
                    return None
                rv = in_type(rv_str)
            except ValueError:
                pass
    return rv