TheAlgorithms/Python · error · ValueError

Invalid value for min_val or max_val (min_value < max_value)

Error message

Invalid value for min_val or max_val (min_value < max_value)

What it means

Raised by temp_input_value when min_val is strictly greater than max_val, since the helper returns one endpoint of a valid integer range and an inverted range has no valid endpoints. The function is used by the guess-the-number binary search to seed its bounds.

Source

Thrown at other/guess_the_number_search.py:54

    >>> temp_input_value("ten","fifty",1)
    Traceback (most recent call last):
        ...
    AssertionError: Invalid type of value(s) specified to function!

    >>> temp_input_value(min_val=-100, max_val=500)
    -100

    >>> temp_input_value(min_val=-5100, max_val=-100)
    -5100
    """
    assert (
        isinstance(min_val, int)
        and isinstance(max_val, int)
        and isinstance(option, bool)
    ), "Invalid type of value(s) specified to function!"

    if min_val > max_val:
        raise ValueError("Invalid value for min_val or max_val (min_value < max_value)")
    return min_val if option else max_val


def get_avg(number_1: int, number_2: int) -> int:
    """
    Return the mid-number(whole) of two integers a and b

    >>> get_avg(10, 15)
    12

    >>> get_avg(20, 300)
    160

    >>> get_avg("abcd", 300)
    Traceback (most recent call last):
        ...
    TypeError: can only concatenate str (not "int") to str

View on GitHub (pinned to f5988cc097)

Solutions

  1. Ensure min_val <= max_val at the call site; swap the arguments if they are reversed
  2. Normalize bounds before calling: lo, hi = sorted((min_val, max_val))

Example fix

# before
val = temp_input_value(min_val=100, max_val=5, option=True)  # ValueError

# after
val = temp_input_value(min_val=5, max_val=100, option=True)
Defensive patterns

Strategy: validation

Validate before calling

def valid_bounds(min_val: int, max_val: int) -> bool:
    return isinstance(min_val, int) and isinstance(max_val, int) and min_val <= max_val

Prevention

When it happens

Trigger: Calling temp_input_value(min_val=100, max_val=5, ...) or any invocation where min_val > max_val. Types must already be int/bool or the preceding assert fires first.

Common situations: Passing user-supplied or config-driven bounds in the wrong order, or swapping arguments positionally (min_val and max_val mixed up).

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/d7c812571c836d94. Report an issue: GitHub.