def try_except(var, d, text1, text2):
    """
    Helps to deal with exceptions

    :param var: string, that needs to be converted to integer
    :param d: right border of possible values for integer
    :param text1: message that will appear if the task number is not integer
    :param text2: message that will appear if the task number is out of range
    :return: integer value of var minus one
    """
    while True:
        try:
            var = int(var) - 1
        except ValueError:
            var = input(text1)
        else:
            if var < 0 or var >= d:
                var = input(text2)
            else:
                return var