Skip to content

Commit 5e36d30

Browse files
authored
Merge pull request #122 from PdxCodeGuild/zach-lab11-contact-list
Zach lab11 contact list
2 parents 9b62cf9 + 05cbe68 commit 5e36d30

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

code/zach/contacts.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Name,Phone,Email
2+
Zachary LaFever,703-555-6666,[email protected]
3+
Sara Beth,703-666-7777,[email protected]
4+
Justin Young, 1234567890, [email protected]

code/zach/lab11-contact-list.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
def version1():
2+
with open('contacts.csv', 'r') as file:
3+
lines = file.read().split('\n')
4+
contacts = []
5+
index = 1
6+
headers = lines[0].split(",")
7+
8+
while index < len(lines):
9+
contact = lines[index].split(",")
10+
contacts.append(dict(zip(headers, contact)))
11+
index += 1
12+
13+
return contacts
14+
15+
16+
def version2_3():
17+
contacts = version1()
18+
query_type = input("What would you like to do?\nCreate\nRetrieve\nUpdate\nDelete\n> ")
19+
20+
if query_type == 'Create':
21+
with open('contacts.csv', 'a') as file:
22+
name = input("What is the contact's name? ")
23+
phone = input("What is the contact's phone? ")
24+
email = input("What is the contact's email? ")
25+
file.write(f'\n{name}, {phone}, {email}')
26+
27+
elif query_type == 'Retrieve':
28+
name = input("What is the contact's name you would like to find? ")
29+
index = 0
30+
31+
while index < len(contacts):
32+
if name == contacts[index]['Name']:
33+
return print(contacts[index])
34+
index += 1
35+
else:
36+
return print("User not found.")
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+
elif update_type == 'Phone':
52+
phone_change = input('What should their phone number be? ')
53+
data = data.replace(contacts[index]['Phone'], phone_change)
54+
elif update_type == 'Email':
55+
email_change = input('What should their email be? ')
56+
data = data.replace(contacts[index]['Email'], email_change)
57+
else:
58+
print('Not a valid selection.')
59+
file.close()
60+
file = open('contacts.csv', 'wt')
61+
file.write(data)
62+
file.close()
63+
64+
index += 1
65+
66+
elif query_type == 'Delete':
67+
name = input("What is the contact's name you would like to delete? ")
68+
index = 0
69+
70+
while index < len(contacts):
71+
if name == contacts[index]['Name']:
72+
del contacts[index]
73+
data = f"{','.join(contacts[0].keys())}\n"
74+
for dict in contacts:
75+
data = data + f"{','.join(dict.values())}\n"
76+
index += 1
77+
file = open('contacts.csv', 'wt')
78+
file.write(data)
79+
file.close
80+
81+
else:
82+
print("Invalid selection.")
83+
84+
def main():
85+
#version1()
86+
version2_3()
87+
88+
main()

0 commit comments

Comments
 (0)