]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Improve the error message for choices(population, 10) (GH-25267)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Tue, 20 Apr 2021 03:29:48 +0000 (20:29 -0700)
committerGitHub <noreply@github.com>
Tue, 20 Apr 2021 03:29:48 +0000 (20:29 -0700)
Lib/random.py

index 0df26645d9e19e82d17711a2e18a1a24be616c01..3a835aef0bc1d4388056e94a39d3a286cbdc67e6 100644 (file)
@@ -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: