TheAlgorithms/Python · error · ValueError

successes must be lower or equal to trials

Error message

successes must be lower or equal to trials

What it means

binomial_distribution(successes, trials, prob) raises ValueError('successes must be lower or equal to trials') when successes > trials — you cannot have more successes than trials in a binomial experiment. This check runs first, before the negativity, type, and probability checks.

Source

Thrown at maths/binomial_distribution.py:21

from math import factorial


def binomial_distribution(successes: int, trials: int, prob: float) -> float:
    """
    Return probability of k successes out of n tries, with p probability for one
    success

    The function uses the factorial function in order to calculate the binomial
    coefficient

    >>> binomial_distribution(3, 5, 0.7)
    0.30870000000000003
    >>> binomial_distribution (2, 4, 0.5)
    0.375
    """
    if successes > trials:
        raise ValueError("""successes must be lower or equal to trials""")
    if trials < 0 or successes < 0:
        raise ValueError("the function is defined for non-negative integers")
    if not isinstance(successes, int) or not isinstance(trials, int):
        raise ValueError("the function is defined for non-negative integers")
    if not 0 < prob < 1:
        raise ValueError("prob has to be in range of 1 - 0")
    probability = (prob**successes) * ((1 - prob) ** (trials - successes))
    # Calculate the binomial coefficient: n! / k!(n-k)!
    coefficient = float(factorial(trials))
    coefficient /= factorial(successes) * factorial(trials - successes)
    return probability * coefficient


if __name__ == "__main__":
    from doctest import testmod

    testmod()
    print("Probability of 2 successes out of 4 trails")

View on GitHub (pinned to f5988cc097)

Solutions

  1. Ensure successes <= trials; verify the signature order (successes, trials, prob).
  2. Clamp successes to trials if overflow is expected and semantically acceptable.
  3. Audit data aggregation if successes legitimately exceeds trials — that indicates double counting.

Example fix

# before
p_x = binomial_distribution(n, k, 0.5)  # swapped args -> 5 > 3

# after
p_x = binomial_distribution(k, n, 0.5)  # successes first
Defensive patterns

Strategy: validation

Validate before calling

if successes > trials:
    raise ValueError(f"successes ({successes}) cannot exceed trials ({trials})")
p = binomial_distribution(successes, trials, prob)

Type guard

def valid_binomial_counts(k: object, n: object) -> bool:
    return isinstance(k, int) and isinstance(n, int) and 0 <= k <= n

Prevention

When it happens

Trigger: binomial_distribution(5, 3, 0.5); swapped arguments like binomial_distribution(trials, successes, p) when successes < trials.

Common situations: Argument-order confusion (the natural reading k successes out of n invites passing (n, k, p)); aggregating successes across batches but trials from only one batch.

Related errors


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