{"record":{"id":"dfffa6de92bef7e7","repo":"TheAlgorithms/Python","slug":"the-function-is-defined-for-non-negative-integers","errorCode":null,"errorMessage":"the function is defined for non-negative integers","messagePattern":"the function is defined for non-negative integers","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/binomial_distribution.py","lineNumber":23,"sourceCode":"\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\n    print(\"with probability of 0.75 is:\", end=\" \")\r\n    print(binomial_distribution(2, 4, 0.75))\r","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/binomial_distribution.py#L5-L41","documentation":"In binomial_distribution, after the successes<=trials check, negative trials or successes raise ValueError('the function is defined for non-negative integers'). Counting experiments cannot have negative counts.","triggerScenarios":"binomial_distribution(-1, 5, 0.5); binomial_distribution(2, -4, 0.5); counts from deltas like trials = end - start where start > end.","commonSituations":"Negative counters from off-by-one loops; parsed user input with minus signs; reusing the same message for the type check below makes grepping ambiguous — read the line number.","solutions":["Validate trials >= 0 and successes >= 0 before calling.","Fix the subtraction that produced the negative count.","Reject negative inputs at the parse boundary with a clearer message."],"exampleFix":"# before\np_x = binomial_distribution(k, n, 0.5)  # n may be negative\n\n# after\nif n < 0 or k < 0:\n    raise ValueError(f\"counts must be non-negative: k={k}, n={n}\")\np_x = binomial_distribution(k, n, 0.5)","handlingStrategy":"validation","validationCode":"if trials < 0 or successes < 0:\n    raise ValueError(f\"counts must be non-negative: k={successes}, n={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 k >= 0 and n >= 0","tryCatchPattern":null,"preventionTips":["The same message is used for the type check — check line numbers when debugging","Fix subtraction-derived counts (end - start) that can go negative"],"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"}