-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (63 loc) · 2.37 KB
/
main.py
File metadata and controls
70 lines (63 loc) · 2.37 KB
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
import csv
def add_contact(name, phone, email):
with open('contacts.csv', mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([name, phone, email])
def remove_contact(name):
contacts = []
with open('contacts.csv', mode='r') as file:
reader = csv.reader(file)
contacts = [row for row in reader if row[0] != name]
with open('contacts.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerows(contacts)
def update_contact(name, new_phone, new_email):
contacts = []
with open('contacts.csv', mode='r') as file:
reader = csv.reader(file)
for row in reader:
if row[0] == name:
row[1] = new_phone
row[2] = new_email
contacts.append(row)
with open('contacts.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerows(contacts)
def display_contacts():
try:
with open('contacts.csv', mode='r') as file:
reader = csv.reader(file)
for row in reader:
print(f"Name: {row[0]}, Phone: {row[1]}, Email: {row[2]}")
except FileNotFoundError:
print("No contacts found. Please add contacts first.")
def main():
while True:
print("\nContact Book")
print("1. Add Contact")
print("2. Remove Contact")
print("3. Update Contact")
print("4. Display Contacts")
print("5. Exit")
choice = input("Choose an option: ")
if choice == '1':
name = input("Enter name: ")
phone = input("Enter phone number: ")
email = input("Enter email: ")
add_contact(name, phone, email)
elif choice == '2':
name = input("Enter the name of the contact to remove: ")
remove_contact(name)
elif choice == '3':
name = input("Enter the name of the contact to update: ")
new_phone = input("Enter new phone number: ")
new_email = input("Enter new email: ")
update_contact(name, new_phone, new_email)
elif choice == '4':
display_contacts()
elif choice == '5':
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()