Menu
Coddy logo textTech

Kontakt löschen

Teil des Abschnitts Logic & Flow der Python-Journey von Coddy — Lektion 21 von 78.

challenge icon

Aufgabe

Einfach

Der nächste Schritt besteht darin, die Funktion delete_contact zu erstellen. Diese Funktion ermöglicht es Benutzern, einen bestimmten Kontakt aus dem Kontaktbuch zu entfernen.


Deine Aufgabe:

  1. Erstelle eine Funktion namens delete_contact, die ein Argument entgegennimmt: contact_book (ein Dictionary).
  2. Frage den Namen des Kontakts ab, den der Benutzer löschen möchte.
  3. Überprüfe, ob der Name im contact_book existiert:
    • Wenn er existiert, entferne den Kontakt aus dem Dictionary.
    • Gib aus: "Contact deleted successfully!".
  4. Wenn der Kontakt nicht existiert, gib aus: "Contact not found!".

Probier es selbst

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!")

Alle Lektionen in Logic & Flow