@@ -4,16 +4,17 @@ def version1():
4
4
contacts = []
5
5
index = 1
6
6
headers = lines [0 ].split ("," )
7
-
7
+
8
8
while index < len (lines ):
9
9
contact = lines [index ].split ("," )
10
10
contacts .append (dict (zip (headers , contact )))
11
11
index += 1
12
-
12
+
13
13
return contacts
14
14
15
- def version2 ():
16
-
15
+
16
+ def version2_3 ():
17
+ contacts = version1 ()
17
18
query_type = input ("What would you like to do?\n Create\n Retrieve\n Update\n Delete\n > " )
18
19
19
20
if query_type == 'Create' :
@@ -22,31 +23,76 @@ def version2():
22
23
phone = input ("What is the contact's phone? " )
23
24
email = input ("What is the contact's email? " )
24
25
file .write (f'\n { name } , { phone } , { email } ' )
25
-
26
+
26
27
elif query_type == 'Retrieve' :
27
- contacts = version1 ()
28
- name = input ("What is the contact's name? " )
29
- index = 0
28
+ name = input ("What is the contact's name you would like to find? " )
29
+ index = 0
30
30
31
31
while index < len (contacts ):
32
32
if name == contacts [index ]['Name' ]:
33
33
return print (contacts [index ])
34
34
index += 1
35
35
else :
36
36
return print ("User not found." )
37
-
38
- #elif query_type == 'Update':
39
-
40
- #elif query_type == 'Delete':
37
+
38
+ elif query_type == 'Update' :
39
+ name = input ("What is the contact's name you would like to update? " )
40
+ index = 0
41
+
42
+ while index < len (contacts ):
43
+ if name == contacts [index ]['Name' ]:
44
+ with open ('contacts.csv' , 'rt' ) as file :
45
+ data = file .read ()
46
+ update_type = input ('Would you like to update their Name, Phone, or Email? ' )
47
+
48
+ if update_type == 'Name' :
49
+ name_change = input ('What should their name change to? ' )
50
+ data = data .replace (contacts [index ]['Name' ], name_change )
51
+ file .close ()
52
+ file = open ('contacts.csv' , 'wt' )
53
+ file .write (data )
54
+ file .close ()
55
+ elif update_type == 'Phone' :
56
+ phone_change = input ('What should their phone number be? ' )
57
+ data = data .replace (contacts [index ]['Phone' ], phone_change )
58
+ file .close ()
59
+ file = open ('contacts.csv' , 'wt' )
60
+ file .write (data )
61
+ file .close ()
62
+ elif update_type == 'Email' :
63
+ email_change = input ('What should their email be? ' )
64
+ data = data .replace (contacts [index ]['Email' ], email_change )
65
+ file .close ()
66
+ file = open ('contacts.csv' , 'wt' )
67
+ file .write (data )
68
+ file .close ()
69
+ else :
70
+ print ('Not a valid selection.' )
71
+
72
+ index += 1
73
+ else :
74
+ return print ("User not found." )
41
75
42
- #else:
43
-
44
- def version3 ():
45
-
46
- return 0
76
+ elif query_type == 'Delete' :
77
+ name = input ("What is the contact's name you would like to delete? " )
78
+ index = 0
79
+
80
+ while index < len (contacts ):
81
+ if name == contacts [index ]['Name' ]:
82
+ del contacts [index ]
83
+ data = f"{ ',' .join (contacts [0 ].keys ())} \n "
84
+ for dict in contacts :
85
+ data = data + f"{ ',' .join (dict .values ())} \n "
86
+ index += 1
87
+ file = open ('contacts.csv' , 'wt' )
88
+ file .write (data )
89
+ file .close
90
+
91
+ else :
92
+ print ("Invalid selection." )
47
93
48
94
def main ():
49
95
#version1()
50
- version2 ()
96
+ version2_3 ()
51
97
52
98
main ()
0 commit comments