Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Contact list created
  • Loading branch information
i-OmSharma authored Oct 29, 2023
1 parent 11af307 commit abb8b46
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions listOfcontact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import json

# Function to load existing contacts from a file
def load_contacts():
try:
with open('contacts.json', 'r') as file:
contacts = json.load(file)
except FileNotFoundError:
contacts = {}
return contacts

# Function to save contacts to a file
def save_contacts(contacts):
with open('contacts.json', 'w') as file:
json.dump(contacts, file)

# Function to add a new contact
def add_contact(name, phone):
contacts = load_contacts()
contacts[name] = phone
save_contacts(contacts)

# Function to view all contacts
def view_contacts():
contacts = load_contacts()
if contacts:
print("Contacts:")
for name, phone in contacts.items():
print(f"{name}: {phone}")
else:
print("No contacts found.")

# Main program loop
while True:
print("\nOptions:")
print("1. Add a contact")
print("2. View contacts")
print("3. Exit")

choice = input("Enter your choice (1/2/3): ")

if choice == '1':
name = input("Enter the name here : ")
phone = input("Enter the phone number: ")
add_contact(name, phone)
print("Contact added successfully!")

elif choice == '2':
view_contacts()

elif choice == '3':
break

else:
print("Invalid choice. Please try again later.")

0 comments on commit abb8b46

Please sign in to comment.