Skip to content

Commit e7c2e99

Browse files
committed
Revert changes to choice()
1 parent b01be91 commit e7c2e99

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

Lib/random.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,11 @@ def _randbelow_without_getrandbits(self, n, int=int, maxsize=1<<BPF):
297297

298298
def choice(self, seq):
299299
"""Choose a random element from a non-empty sequence."""
300-
n = len(seq)
301-
if n == 0:
302-
raise IndexError('Cannot choose from an empty sequence')
303-
return seq[self._randbelow(n)]
300+
try:
301+
i = self._randbelow(len(seq))
302+
except ValueError:
303+
raise IndexError('Cannot choose from an empty sequence') from None
304+
return seq[i]
304305

305306
def shuffle(self, x, random=None):
306307
"""Shuffle list x in place, and return None.

0 commit comments

Comments
 (0)