File tree Expand file tree Collapse file tree 5 files changed +85
-0
lines changed Expand file tree Collapse file tree 5 files changed +85
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Grocery shopping
2
+
3
+ You enter with items to be store in a ` .data ` file
4
+
5
+ ### Worked concepts
6
+ * File reading and writing
7
+ * Pickle
Original file line number Diff line number Diff line change
1
+
2
+ grocery_list = []
3
+
4
+
5
+ def add (item ):
6
+ grocery_list .append (item )
7
+
8
+
9
+ def add (* items ):
10
+ for i in items :
11
+ grocery_list .append (i )
12
+
13
+
14
+ def exclude (item ):
15
+ grocery_list .remove (item )
16
+
17
+
18
+ def get_list ():
19
+ return grocery_list
20
+
21
+
22
+ def alter (item , new_item ):
23
+ grocery_list [grocery_list .index (item )] = new_item
Original file line number Diff line number Diff line change
1
+ import pickle
2
+
3
+ file_name = "grocery-list.data"
4
+
5
+
6
+ def persist (items ):
7
+ with open (file_name , "wb" ) as f :
8
+ pickle .dump (items , f )
9
+
10
+
11
+ def read ():
12
+ items = []
13
+ with open (file_name , "rb" ) as f :
14
+ items = pickle .load (f )
15
+ return items
16
+
17
+
18
+ def update (items ):
19
+ with open (file_name , "r+b" ) as f :
20
+ pickle .dump (items , f )
Original file line number Diff line number Diff line change
1
+ import grocery_list
2
+ import grocery_list_repository
3
+
4
+
5
+ def main ():
6
+ option = 0
7
+ while True :
8
+ print ("1 - Add item to the list" )
9
+ print ("2 - Update item from the list" )
10
+ print ("3 - Delete item from the list" )
11
+ print ("4 - Get all items from the list" )
12
+ print ("5 - Save list" )
13
+ print ("0 - Exit" )
14
+ option = int (input ("> " ))
15
+
16
+ if option == 1 :
17
+ item = input ("Enter the item: " )
18
+ grocery_list .add (item .title ())
19
+ elif option == 2 :
20
+ item = input ("Enter the item you want to replace: " )
21
+ new_item = input ("Enter the new item to store on the list: " )
22
+ grocery_list .alter (item , new_item )
23
+ elif option == 3 :
24
+ item = input ("Enter the item you want to remove: " )
25
+ grocery_list .exclude (item )
26
+ elif option == 4 :
27
+ print ("Items - " )
28
+ print (grocery_list_repository .read ())
29
+ elif option == 5 :
30
+ grocery_list_repository .persist (grocery_list .get_list ())
31
+ elif option == 0 :
32
+ break
33
+
34
+ if __name__ == '__main__' :
35
+ main ()
You can’t perform that action at this time.
0 commit comments