-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contact.py
63 lines (53 loc) · 1.7 KB
/
Contact.py
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
import os
CONTACTS_FILE = 'contacts.txt'
def load_contacts():
if not os.path.exists(CONTACTS_FILE):
return []
with open(CONTACTS_FILE, 'r') as file:
return [line.strip().split('|') for line in file]
def save_contacts(contacts):
with open(CONTACTS_FILE, 'w') as file:
for contact in contacts:
file.write('|'.join(contact) + '\n')
def add_contact(contacts):
name = input("Enter name: ")
phone = input("Enter phone number: ")
email = input("Enter email address: ")
contacts.append([name, phone, email])
save_contacts(contacts)
print("Contact added.")
def view_contacts(contacts):
if not contacts:
print("No contacts found.")
for i, contact in enumerate(contacts, start=1):
print(f"{i}. {contact[0]} | {contact[1]} | {contact[2]}")
def delete_contact(contacts):
view_contacts(contacts)
index = int(input("Enter the number of the contact to delete: ")) - 1
if 0 <= index < len(contacts):
contacts.pop(index)
save_contacts(contacts)
print("Contact deleted.")
else:
print("Invalid selection.")
def main():
contacts = load_contacts()
while True:
print("\nContact Manager")
print("1. Add Contact")
print("2. View Contacts")
print("3. Delete Contact")
print("4. Exit")
choice = input("Choose an option: ")
if choice == '1':
add_contact(contacts)
elif choice == '2':
view_contacts(contacts)
elif choice == '3':
delete_contact(contacts)
elif choice == '4':
break
else:
print("Invalid choice.")
if __name__ == "__main__":
main()