From: Raymond Hettinger Date: Tue, 20 Apr 2021 03:29:48 +0000 (-0700) Subject: Improve the error message for choices(population, 10) (GH-25267) X-Git-Tag: v3.10.0b1~233 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2a36b09ce7cd67503893412b53295717d66fee19;p=thirdparty%2FPython%2Fcpython.git Improve the error message for choices(population, 10) (GH-25267) --- diff --git a/Lib/random.py b/Lib/random.py index 0df26645d9e1..3a835aef0bc1 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: