-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshopping_list.py
291 lines (207 loc) · 7.96 KB
/
shopping_list.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""
Shopping List (Hackbright Prep dictionary code challenge)
Create a dictionary of shopping lists, and be able to
* add / remove lists
* add / remove items from lists
"""
def add_new_shopping_list(lists_by_name, new_list_name):
"""Add new shopping list to dict: key is list name, value is empty list
Will not allow duplicate shopping list names to be added. This method
is case-sensitive.
Arguments:
lists_by_name: dict of shopping lists
new_list_name: string name of the new list to add
Returns:
None
"""
# your code here!
#pass
lists_by_name[new_list_name]=[]
def remove_shopping_list(lists_by_name, list_name_to_remove):
"""Remove shopping list from shopping lists dict.
If the shopping list name does not exist in the dictionary, print a message
and do nothing. This method is case-sensitive.
Arguments:
lists_by_name: dict of shopping lists
list_name_to_remove: string name of the list to remove
Returns:
None
"""
# your code here!
del lists_by_name[list_name_to_remove]
def add_to_shopping_list(lists_by_name, list_name, items):
"""Add given items to shopping list.
Arguments:
lists_by_name: dict of shopping lists
list_name: string name of a shopping list
items: list of items to add to the list
Returns:
None
"""
lists_by_name[list_name] = lists_by_name[list_name] + items
# your code here!
def remove_from_shopping_list(lists_by_name, list_name, items):
"""Remove given items from shopping list.
If an item doesn't exist in the list, print an error, and continue to
attempt to remove the other items.
Arguments:
lists_by_name: dict of shopping lists
list_name: string name of a shopping list
items: list of items to remove from the list
Returns:
None
"""
# your code here!
#lists_by_name[list_name.remove(items)]
items in lists_by_name.values()
if True:
del lists_by_name[list_name(items)]
else:
print "That item doesn't exist in any of the lists"
def display_shopping_list(lists_by_name, list_name):
"""Print the contents of a shopping list.
If the list is missing, return a string message saying so. This function is
case sensitive.
Arguments:
lists_by_name: dict of shopping lists
list_name: string name of a shopping list
Returns:
None
"""
if list_name in lists_by_name.keys():
print lists_by_name[list_name]
else:
print "No such list"
# There is no list with name " + list_name
#else:
# your code here!
#for store in lists_by_name.keys():
# if list_name in store:
# print list_name + ": " + lists_by_name[list_name]
# break
# else:
# print "There is no list by " + list_name
def show_all_lists(lists_by_name):
"""Given a dictionary of shopping lists, print out each list.
Arguments:
lists_by_name: dict of shopping lists
Returns:
None
"""
# your code here!
for key,value in lists_by_name.items():
print key,value
def parse_string_of_items(items_string):
"""Split input sting on commas and return the list of items.
Trim leading and trailing whitespace.
Arguments:
items_string: a string with 0 or more commas separating items
Returns:
list of strings
"""
# list to return, starts out empty
items = []
# split the items_string on commas into a temporary list
temp_items = items_string.split(',')
# iterate through the temporary list and strip white space from each item
# before appending it to the list to be returned
for item in temp_items:
items.append(item.strip())
return items
def edit_shopping_list(lists_by_name, list_name, add_or_remove):
"""Get items from user and add / remove them from the shopping list
Arguments:
lists_by_name: dict of shopping lists
list_name: string name of a shopping list
add_or_remove: string that is either 'add' or 'remove', indicating whether
the collected items should be added to or removed from the list
Returns:
None
"""
# if so, get items to add to the list
input_str = raw_input('Please enter items separated by commas: ')
# list-ify the input string
items = parse_string_of_items(input_str)
# add or remove, according to the argument
if add_or_remove == 'add':
add_to_shopping_list(shopping_lists_by_name, list_name, items)
else:
remove_from_shopping_list(shopping_lists_by_name, list_name, items)
def get_menu_choice():
"""Print a menu and asks the user to make a choice.
Arguments:
None
Returns:
int: the user's menu choice
"""
print '\n 0 - Main Menu'
print ' 1 - Show all lists.'
print ' 2 - Show a specific list.'
print ' 3 - Add a new shopping list.'
print ' 4 - Add item(s) to a shopping list.'
print ' 5 - Remove items(s) from a shopping list.'
print ' 6 - Remove a list by name.'
print ' 7 - Exit the program.\n'
choice = int(raw_input('Choose from the menu options: '))
return choice
def execute_repl(shopping_lists_by_name):
"""Execute the repl loop for the control structure of the program.
(REPL stands for Read - Eval - Print Loop. For more info:
https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop)
Arguments:
shopping_lists_by_name: dict of shopping lists
Returns:
None
"""
while True:
# get the next choice from the user
choice = get_menu_choice()
if choice == 0:
# main menu
continue # continue goes to the next loop iteration
elif choice == 1:
# show all lists
# call function to display all lists
show_all_lists(shopping_lists_by_name)
elif choice == 3:
# Add a new shopping list
# get name of list and add it
list_name = raw_input('Enter the name for your list: ')
add_new_shopping_list(shopping_lists_by_name, list_name)
# get items for list and add them
input_str = raw_input('Please enter items separated by commas: ')
items = parse_string_of_items(input_str)
shopping_lists_by_name[list_name] = items
elif choice == 7:
# quit
break
else:
# all of the remaning choices require an existing list. First, run
# code to get the list name from the user and verify it exists in
# the dict
# determine which list
list_name = raw_input('Which list would you like to see? ')
# test to see if the list is in the shopping list dict
if list_name not in shopping_lists_by_name:
# no list by this name :-(
print 'There is no {} list.'.format(list_name)
continue
# if the code reaches this point, it means the list exists in the
# dictionary, so proceed according to which choice was chosen
if choice == 2:
# show a specific list
display_shopping_list(shopping_lists_by_name, list_name)
elif choice == 4:
# Add item(s) to a shopping list
# add items to the shopping list
edit_shopping_list(shopping_lists_by_name, list_name, 'add')
elif choice == 5:
# Remove an item from a shopping list
# add items to the shopping list
edit_shopping_list(shopping_lists_by_name, list_name, 'remove')
elif choice == 6:
# remove list
remove_shopping_list(shopping_lists_by_name, list_name)
# Main code here
shopping_lists_by_name = {} # key is list name, value is [shopping list]
execute_repl(shopping_lists_by_name)