{"record":{"id":"851738ea2759b8b8","repo":"TheAlgorithms/Python","slug":"successes-must-be-lower-or-equal-to-trials","errorCode":null,"errorMessage":"successes must be lower or equal to trials","messagePattern":"successes must be lower or equal to trials","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/binomial_distribution.py","lineNumber":21,"sourceCode":"\r\nfrom math import factorial\r\n\r\n\r\ndef binomial_distribution(successes: int, trials: int, prob: float) -> float:\r\n    \"\"\"\r\n    Return probability of k successes out of n tries, with p probability for one\r\n    success\r\n\r\n    The function uses the factorial function in order to calculate the binomial\r\n    coefficient\r\n\r\n    >>> binomial_distribution(3, 5, 0.7)\r\n    0.30870000000000003\r\n    >>> binomial_distribution (2, 4, 0.5)\r\n    0.375\r\n    \"\"\"\r\n    if successes > trials:\r\n        raise ValueError(\"\"\"successes must be lower or equal to trials\"\"\")\r\n    if trials < 0 or successes < 0:\r\n        raise ValueError(\"the function is defined for non-negative integers\")\r\n    if not isinstance(successes, int) or not isinstance(trials, int):\r\n        raise ValueError(\"the function is defined for non-negative integers\")\r\n    if not 0 < prob < 1:\r\n        raise ValueError(\"prob has to be in range of 1 - 0\")\r\n    probability = (prob**successes) * ((1 - prob) ** (trials - successes))\r\n    # Calculate the binomial coefficient: n! / k!(n-k)!\r\n    coefficient = float(factorial(trials))\r\n    coefficient /= factorial(successes) * factorial(trials - successes)\r\n    return probability * coefficient\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    from doctest import testmod\r\n\r\n    testmod()\r\n    print(\"Probability of 2 successes out of 4 trails\")\r","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/binomial_distribution.py#L3-L39","documentation":"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.","triggerScenarios":"binomial_distribution(5, 3, 0.5); swapped arguments like binomial_distribution(trials, successes, p) when successes < trials.","commonSituations":"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.","solutions":["Ensure successes <= trials; verify the signature order (successes, trials, prob).","Clamp successes to trials if overflow is expected and semantically acceptable.","Audit data aggregation if successes legitimately exceeds trials — that indicates double counting."],"exampleFix":"# before\np_x = binomial_distribution(n, k, 0.5)  # swapped args -> 5 > 3\n\n# after\np_x = binomial_distribution(k, n, 0.5)  # successes first","handlingStrategy":"validation","validationCode":"if successes > trials:\n    raise ValueError(f\"successes ({successes}) cannot exceed trials ({trials})\")\np = binomial_distribution(successes, trials, prob)","typeGuard":"def valid_binomial_counts(k: object, n: object) -> bool:\n    return isinstance(k, int) and isinstance(n, int) and 0 <= k <= n","tryCatchPattern":null,"preventionTips":["Argument order is (successes, trials, prob) — k first, n second","successes > trials usually means swapped args or double-counted successes"],"tags":["math","probability","statistics","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}