summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-04-19 23:15:50 -0700
committerGitHub <noreply@github.com>2021-04-19 23:15:50 -0700
commitfa03efda3dc6ad118788bebc61079cd481c0b24c (patch)
treea48823f5d9eba0fa5c3b542032992c8372ac7e11
parent072ec69af592611f36349f5048569ab7e72b8b61 (diff)
downloadcpython-git-fa03efda3dc6ad118788bebc61079cd481c0b24c.tar.gz
Improve the error message for choices(population, 10) (GH-25267) (GH-25477)
-rw-r--r--Lib/random.py10
1 files changed, 9 insertions, 1 deletions
diff --git a/Lib/random.py b/Lib/random.py
index 36e16a9063..53252764a5 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -485,7 +485,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: