There was an error while loading. Please reload this page.
1 parent 072ec69 commit fa03efdCopy full SHA for fa03efd
1 file changed
Lib/random.py
@@ -485,7 +485,15 @@ def choices(self, population, weights=None, *, cum_weights=None, k=1):
485
floor = _floor
486
n += 0.0 # convert to float for a small speed improvement
487
return [population[floor(random() * n)] for i in _repeat(None, k)]
488
- cum_weights = list(_accumulate(weights))
+ try:
489
+ cum_weights = list(_accumulate(weights))
490
+ except TypeError:
491
+ if not isinstance(weights, int):
492
+ raise
493
+ k = weights
494
+ raise TypeError(
495
+ f'The number of choices must be a keyword argument: {k=}'
496
+ ) from None
497
elif weights is not None:
498
raise TypeError('Cannot specify both weights and cumulative weights')
499
if len(cum_weights) != n:
0 commit comments