def int_or_none(x, limit):
    """Returns `int(x)` if `x` is a valid `int` or `None` otherwise.

    `x` is valid if `1 <= x <= limit`.
    """
    try:
        value = int(x)
        if 1 <= value <= limit:
            return value
        else:
            return None
    except ValueError:
        return None