There was an error while loading. Please reload this page.
1 parent 503cdc7 commit 2a36b09Copy full SHA for 2a36b09
1 file changed
Lib/random.py
@@ -518,7 +518,15 @@ def choices(self, population, weights=None, *, cum_weights=None, k=1):
518
floor = _floor
519
n += 0.0 # convert to float for a small speed improvement
520
return [population[floor(random() * n)] for i in _repeat(None, k)]
521
- cum_weights = list(_accumulate(weights))
+ try:
522
+ cum_weights = list(_accumulate(weights))
523
+ except TypeError:
524
+ if not isinstance(weights, int):
525
+ raise
526
+ k = weights
527
+ raise TypeError(
528
+ f'The number of choices must be a keyword argument: {k=}'
529
+ ) from None
530
elif weights is not None:
531
raise TypeError('Cannot specify both weights and cumulative weights')
532
if len(cum_weights) != n:
0 commit comments