Skip to content

Commit aae8c8a

Browse files
Saving progress on lab 11.
1 parent 2470600 commit aae8c8a

File tree

2 files changed

+67
-21
lines changed

2 files changed

+67
-21
lines changed

code/zach/contacts.csv

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Name,Phone,Email
2-
Zachary Christman,703-555-6666,[email protected]
3-
Sara Beth,703-666-7777,sarabeth@test.comJustin Young, 1234567890, JustinYoung@test.com
4-
Justin Young, 1234567890, [email protected]
2+
Zachary LaFever,703-555-6666,[email protected]
3+
Sara Beth,703-666-7777,[email protected]
4+
Justin Old, 1234567890, [email protected]

code/zach/lab11-contact-list.py

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ def version1():
44
contacts = []
55
index = 1
66
headers = lines[0].split(",")
7-
7+
88
while index < len(lines):
99
contact = lines[index].split(",")
1010
contacts.append(dict(zip(headers, contact)))
1111
index += 1
12-
12+
1313
return contacts
1414

15-
def version2():
16-
15+
16+
def version2_3():
17+
contacts = version1()
1718
query_type = input("What would you like to do?\nCreate\nRetrieve\nUpdate\nDelete\n> ")
1819

1920
if query_type == 'Create':
@@ -22,31 +23,76 @@ def version2():
2223
phone = input("What is the contact's phone? ")
2324
email = input("What is the contact's email? ")
2425
file.write(f'\n{name}, {phone}, {email}')
25-
26+
2627
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
3030

3131
while index < len(contacts):
3232
if name == contacts[index]['Name']:
3333
return print(contacts[index])
3434
index += 1
3535
else:
3636
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.")
4175

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.")
4793

4894
def main():
4995
#version1()
50-
version2()
96+
version2_3()
5197

5298
main()

0 commit comments

Comments
 (0)