forked from fluentpython/example-code-2e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlotto.py
More file actions
30 lines (19 loc) · 634 Bytes
/
lotto.py
File metadata and controls
30 lines (19 loc) · 634 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# tag::LOTTERY_BLOWER[]
import random
from tombola import Tombola
class LottoBlower(Tombola):
def __init__(self, iterable):
self._balls = list(iterable) # <1>
def load(self, iterable):
self._balls.extend(iterable)
def pick(self):
try:
position = random.randrange(len(self._balls)) # <2>
except ValueError:
raise LookupError('pick from empty LottoBlower')
return self._balls.pop(position) # <3>
def loaded(self): # <4>
return bool(self._balls)
def inspect(self): # <5>
return tuple(self._balls)
# end::LOTTERY_BLOWER[]