-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintermediate_python.py
More file actions
73 lines (58 loc) · 2.37 KB
/
Copy pathintermediate_python.py
File metadata and controls
73 lines (58 loc) · 2.37 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Groceries:
"""
A grocery list with ability to add or remove items, as well as check them off the list.
"""
def __init__(self):
"""
Instance is initialized as an empty dictionary.
"""
self.grocery_list = {}
def add_item(self,item):
"""
Adds an item to the grocery list, with value initialized to False.
Args:
item: the item to be added to the list.
Raises:
TypeError: If the input is not of type String
"""
if type(item) != str :
raise TypeError('Items to be added must be of type String')
else:
self.grocery_list.update({item : False})
def remove_item(self,item):
"""
Removes an item from the grocery list.
Args:
item: the item to be removed from the list.
Raises:
TypeError: If the input is not of type String.
ValueError: If the item does not exist in the list.
"""
if type(item) != str :
raise TypeError('Items to be added must be of type String')
elif item not in self.grocery_list:
raise ValueError('There is no item named %s in the list' % item)
else:
self.grocery_list.pop(item)
def check_item(self,item):
"""
Set the value of an item to True to indicate it has been purchased.
It does not check if the value is already set to True.
Args:
item: the item to be checked off the list.
Raises:
TypeError: If the input is not of type String
ValueError: If the item does not exist in the list.
"""
if type(item) != str :
raise TypeError('Items to be checked must be of type String')
elif item not in self.grocery_list:
raise ValueError('There is no item named %s in the list' % item)
else:
self.grocery_list.update({item : True})
def items_remaining(self):
"""
Returns the number of unchecked items (False as the value) on the list
Returns: The number of unchecked items on the list
"""
return sum([1 for x in self.grocery_list.values() if x==False])