Contact book creation in python
Contact book is a collection of data which has the list of people name and contact details. It is an evergreen concept for file handling. It gets the user input, process it and display the contact.
Let us create contact book.
It has following activities
- · Add new contact
- · Save contacts to a file.
- · Search a contact
- · Display the contact details.
- · Delete a contact
The python program to create a Contact Book :
contactbook = {}
def add_it():
c_name =
input("Enter name: ")
c_phone =
input("Enter phone number: ")
c_email =
input("Enter email: ")
contactbook[c_name] = {"phone Number": c_phone, "email
id": c_email}
print(f"{c_name} is added successfully!")
def search_it():
name =
input("Enter the name to search: ")
if name in
contactbook:
print(f"Name: {name}, Phone: {contactbook[name]['phone Number']},
Email: {contactbook[name]['email id']}")
else:
print("Contact is not found.")
def display_it():
if contactbook:
for c_name in
contactbook.items():
print(f"{c_name} → Phone: {'phone number'} ,Email: {'email
id'}")
else:
print("No
contacts available.")
def delete_it():
c_name =
input("Enter name to delete: ")
if c_name in
contactbook:
del
contactbook[c_name]
print(f"{c_name} is deleted successfully!")
else:
print("Contact is not found.")
# Menu Loop
while True:
print("\n---
Contact Book Menu ---")
print("1. Add
Contact")
print("2.
Search Contact")
print("3.
Display Contacts")
print("4.
Delete Contact")
print("5.
Exit")
choice =
input("Enter your choice (1-5): ")
if choice ==
"1":
add_it()
elif choice ==
"2":
search_it()
elif choice ==
"3":
display_it()
elif choice ==
"4":
delete_it()
elif choice ==
"5":
print("Exiting Contact Book. Goodbye!")
break
else:
print("Invalid choice. Try again.")
Output:
--- Contact Book Menu ---
1. Add Contact
2. Search Contact
3. Display Contacts
4. Delete Contact
5. Exit
Enter your choice (1-5): 1
Enter name: raj
Enter phone number: 123456789
Enter email: raj@123.com
raj is added successfully!
--- Contact Book Menu ---
1. Add Contact
2. Search Contact
3. Display Contacts
4. Delete Contact
5. Exit
Enter your choice (1-5): 1
Enter name: jey
Enter phone number: 2314567540
Enter email: jey@gmail.com
jey is added successfully!
--- Contact Book Menu ---
1. Add Contact
2. Search Contact
3. Display Contacts
4. Delete Contact
5. Exit
Enter your choice (1-5): 2
Enter the name to search: raj
Name: raj, Phone: 123456789, Email: raj@123.com
--- Contact Book Menu ---
1. Add Contact
2. Search Contact
3. Display Contacts
4. Delete Contact
5. Exit
Enter your choice (1-5): 3
('raj', {'phone Number': '123456789', 'email id':
'raj@123.com'})
('jey', {'phone Number': '2314567540', 'email id':
'jey@gmail.com'})
--- Contact Book Menu ---
1. Add Contact
2. Search Contact
3. Display Contacts
4. Delete Contact
5. Exit
Enter your choice (1-5): 4
Enter name to delete: raj
raj is deleted successfully!
--- Contact Book Menu ---
1. Add Contact
2. Search Contact
3. Display Contacts
4. Delete Contact
5. Exit
Enter your choice (1-5): 3
('jey', {'phone Number': '2314567540', 'email id':
'jey@gmail.com'})
--- Contact Book Menu ---
1. Add Contact
2. Search Contact
3. Display Contacts
4. Delete Contact
5. Exit
Enter your choice (1-5): 5
Exiting Contact Book. Goodbye!
Hope, this code is useful to you. Keep coding!!!!
Comments
Post a Comment