-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjar.py
More file actions
28 lines (22 loc) · 706 Bytes
/
jar.py
File metadata and controls
28 lines (22 loc) · 706 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
class Jar:
def __init__(self, capacity=12):
if not isinstance(capacity, int) or capacity < 0:
raise ValueError("Capacity must be a non-negative integer.")
self._capacity = capacity
self._size = 0
def __str__(self):
return "🍪" * self._size
def deposit(self, n):
if self._size + n > self._capacity:
raise ValueError("Too many cookies!")
self._size += n
def withdraw(self, n):
if self._size - n < 0:
raise ValueError("Not enough cookies!")
self._size -= n
@property
def capacity(self):
return self._capacity
@property
def size(self):
return self._size