TheAlgorithms/Python · error · ValueError
Please enter positive integers for n and k where n >= k
Error message
Please enter positive integers for n and k where n >= k
What it means
Raised by combinations() in maths/combinations.py when asked for a binomial coefficient that would require a factorial of a negative number: specifically n < k or k < 0. The multiplicative formula res *= n - i over range(k) only makes combinatorial sense for 0 <= k <= n, so the function guards that up front with ValueError.
Source
Thrown at maths/combinations.py:36
>>> combinations(20,5)
15504
>>> combinations(52, 5)
2598960
>>> combinations(0, 0)
1
>>> combinations(-4, -5)
...
Traceback (most recent call last):
ValueError: Please enter positive integers for n and k where n >= k
"""
# If either of the conditions are true, the function is being asked
# to calculate a factorial of a negative number, which is not possible
if n < k or k < 0:
raise ValueError("Please enter positive integers for n and k where n >= k")
res = 1
for i in range(k):
res *= n - i
res //= i + 1
return res
if __name__ == "__main__":
print(
"The number of five-card hands possible from a standard",
f"fifty-two card deck is: {combinations(52, 5)}\n",
)
print(
"If a class of 40 students must be arranged into groups of",
f"4 for group projects, there are {combinations(40, 4)} ways",
"to arrange them.\n",
)View on GitHub (pinned to f5988cc097)
Solutions
- Check the argument order — the signature is combinations(n, k) with n total items and k chosen.
- Clamp k with k = min(k, n) or return 0 by convention when k > n, mirroring math.comb.
- Prefer the standard library math.comb(n, k) which returns 0 for k > n instead of raising.
Example fix
# before combinations(3, 5) # ValueError # after from math import comb result = comb(3, 5) # 0, no exception # or guard: n, k = max(n, k), min(n, k) if args may be swapped
Defensive patterns
Strategy: validation
Validate before calling
if k < 0 or n < k:
raise ValueError(f'need 0 <= k <= n, got n={n}, k={k}')
result = combinations(n, k) Try / catch
try:
result = combinations(n, k)
except ValueError:
result = 0 # mirror math.comb convention for k > n, if that suits your domain Prevention
- Prefer math.comb which returns 0 for k > n instead of raising.
- Assert 0 <= k <= n when n and k come from different list lengths.
When it happens
Trigger: Calling combinations(-4, -5), combinations(3, 5) (k > n), or combinations(10, -1). Note combinations(0, 0) is valid and returns 1, and combinations(n, 0) returns 1.
Common situations: Swapped arguments (combinations(k, n)); loop bounds where the upper index comes from a shorter list than the selection size (e.g. choosing 5 items from a 3-item list); negative k from subtracting sizes (len(a) - len(b) when b is longer).
Related errors
- Both points must have the same dimension.
- Monogons and Digons are not polygons in the Euclidean space
- All values must be greater than 0
- Undefined for non-natural numbers
- Please enter a valid number
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/875a81a1f8e30cc7.
Report an issue: GitHub.