Menu
Coddy logo textTech

Delete Contact

Part of the Logic & Flow section of Coddy's Python journey — lesson 21 of 78.

challenge icon

Challenge

Easy

The next step is to create the delete_contact function. This function will allow users to remove a specific contact from the Contact Book.


Your Task:

  1. Create a function named delete_contact that takes one argument: contact_book (a dictionary).
  2. Get input for the contact's name that the user wants to delete.
  3. Check if the name exists in the contact_book:
    • If it exists, remove the contact from the dictionary.
    • Print: "Contact deleted successfully!".
  4. If the contact does not exist, print: "Contact not found!".

Try it yourself

def display_menu():
    print("Contact Book Menu:")
    print("1. Add Contact")
    print("2. View Contact")
    print("3. Edit Contact")
    print("4. Delete Contact")
    print("5. List All Contacts")
    print("6. Exit")

def add_contact(contact_book):
    name = input()
    if name in contact_book:
        print("Contact already exists!")
        return
    phone = input()
    email = input()
    address = input()
    contact_book[name] = {"phone": phone, "email": email, "address": address}
    print("Contact added successfully!")

def view_contact(contact_book):
    name = input()
    if name in contact_book:
        contact = contact_book[name]
        print(f"Name: {name}")
        print(f"Phone: {contact['phone']}")
        print(f"Email: {contact['email']}")
        print(f"Address: {contact['address']}")
    else:
        print("Contact not found!")

def edit_contact(contact_book):
    name = input()
    if name in contact_book:
        phone = input()
        email = input()
        address = input()

        if phone == '':
            phone = contact_book[name]['phone']
        if email == '':
            email = contact_book[name]['email']
        if address == '':
            address = contact_book[name]['address']

        contact_book[name] = {"phone": phone, "email": email, "address": address}
        print("Contact updated successfully!")
    else:
        print("Contact not found!")

All lessons in Logic & Flow