Skip to content

Commit abb8b46

Browse files
authored
Contact list created
1 parent 11af307 commit abb8b46

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

listOfcontact.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import json
2+
3+
# Function to load existing contacts from a file
4+
def load_contacts():
5+
try:
6+
with open('contacts.json', 'r') as file:
7+
contacts = json.load(file)
8+
except FileNotFoundError:
9+
contacts = {}
10+
return contacts
11+
12+
# Function to save contacts to a file
13+
def save_contacts(contacts):
14+
with open('contacts.json', 'w') as file:
15+
json.dump(contacts, file)
16+
17+
# Function to add a new contact
18+
def add_contact(name, phone):
19+
contacts = load_contacts()
20+
contacts[name] = phone
21+
save_contacts(contacts)
22+
23+
# Function to view all contacts
24+
def view_contacts():
25+
contacts = load_contacts()
26+
if contacts:
27+
print("Contacts:")
28+
for name, phone in contacts.items():
29+
print(f"{name}: {phone}")
30+
else:
31+
print("No contacts found.")
32+
33+
# Main program loop
34+
while True:
35+
print("\nOptions:")
36+
print("1. Add a contact")
37+
print("2. View contacts")
38+
print("3. Exit")
39+
40+
choice = input("Enter your choice (1/2/3): ")
41+
42+
if choice == '1':
43+
name = input("Enter the name here : ")
44+
phone = input("Enter the phone number: ")
45+
add_contact(name, phone)
46+
print("Contact added successfully!")
47+
48+
elif choice == '2':
49+
view_contacts()
50+
51+
elif choice == '3':
52+
break
53+
54+
else:
55+
print("Invalid choice. Please try again later.")

0 commit comments

Comments
 (0)