mirror of
https://github.com/python/cpython.git
synced 2025-07-09 20:35:26 +00:00
Improve the error message for choices(population, 10) (GH-25267)
This commit is contained in:
parent
503cdc7c12
commit
2a36b09ce7
1 changed files with 9 additions and 1 deletions
|
@ -518,7 +518,15 @@ class Random(_random.Random):
|
|||
floor = _floor
|
||||
n += 0.0 # convert to float for a small speed improvement
|
||||
return [population[floor(random() * n)] for i in _repeat(None, k)]
|
||||
cum_weights = list(_accumulate(weights))
|
||||
try:
|
||||
cum_weights = list(_accumulate(weights))
|
||||
except TypeError:
|
||||
if not isinstance(weights, int):
|
||||
raise
|
||||
k = weights
|
||||
raise TypeError(
|
||||
f'The number of choices must be a keyword argument: {k=}'
|
||||
) from None
|
||||
elif weights is not None:
|
||||
raise TypeError('Cannot specify both weights and cumulative weights')
|
||||
if len(cum_weights) != n:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue