def f_loop(value, minval, maxval):
    """Returns a number that loops between the min and max."""
    if minval <= value <= maxval:
        return value
    while value <= minval:
        value = maxval - (minval - value) + 1
    while value >= maxval:
        value = minval + (value - maxval) - 1
    return value