diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2021-04-20 03:29:48 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-20 03:29:48 (GMT) |
commit | 2a36b09ce7cd67503893412b53295717d66fee19 (patch) | |
tree | 59c511ff2d8bb49e43bca284d2098df906cac214 /Lib | |
parent | 503cdc7c124cebbd777008bdf7bd9aa666b25f07 (diff) | |
download | cpython-2a36b09ce7cd67503893412b53295717d66fee19.zip cpython-2a36b09ce7cd67503893412b53295717d66fee19.tar.gz cpython-2a36b09ce7cd67503893412b53295717d66fee19.tar.bz2 |
Improve the error message for choices(population, 10) (GH-25267)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/random.py | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/Lib/random.py b/Lib/random.py index 0df2664..3a835ae 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -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: |