TheAlgorithms/Python · error · ValueError

argument value for lower and higher must be(lower > higher)

Error message

argument value for lower and higher must be(lower > higher)

What it means

Raised by guess_the_number when the lower bound exceeds the higher bound, since the binary search between lower and higher requires a well-ordered interval. The check runs after the type assert and before the to_guess range check.

Source

Thrown at other/guess_the_number_search.py:114

        ...
    AssertionError: argument values must be type of "int"

    >>> guess_the_number(10, 1000, 5)
    Traceback (most recent call last):
        ...
    ValueError: guess value must be within the range of lower and higher value

    >>> guess_the_number(10000, 100, 5)
    Traceback (most recent call last):
        ...
    ValueError: argument value for lower and higher must be(lower > higher)
    """
    assert (
        isinstance(lower, int) and isinstance(higher, int) and isinstance(to_guess, int)
    ), 'argument values must be type of "int"'

    if lower > higher:
        raise ValueError("argument value for lower and higher must be(lower > higher)")

    if not lower < to_guess < higher:
        raise ValueError(
            "guess value must be within the range of lower and higher value"
        )

    def answer(number: int) -> str:
        """
        Returns value by comparing with entered `to_guess` number
        """
        if number > to_guess:
            return "high"
        elif number < to_guess:
            return "low"
        else:
            return "same"

    print("started...")

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass the smaller bound first: guess_the_number(lower, higher, to_guess) with lower <= higher
  2. Validate/swap bounds before the call: if lower > higher: lower, higher = higher, lower

Example fix

# before
guess_the_number(100, 10, 5)  # lower=100 > higher=10 -> ValueError

# after
guess_the_number(10, 100, 5)
Defensive patterns

Strategy: validation

Validate before calling

def valid_range(lower: int, higher: int) -> bool:
    return lower <= higher

Prevention

When it happens

Trigger: Calling guess_the_number(100, 10, 5) — i.e. lower=100, higher=10 — or any invocation with lower > higher.

Common situations: Swapping the two bound arguments positionally, or deriving bounds from user input / a config where the order is not enforced.

Related errors


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