Menu
Coddy logo textTech

Alles zusammenfügen

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

challenge icon

Aufgabe

Einfach

Nachdem du nun alle einzelnen Funktionen für das Kontaktbuch erstellt hast, ist es an der Zeit, sie zusammenzufügen, um das vollständige Programm zu erstellen!


Deine Aufgabe:

  1. Kombiniere die Funktionen, die du erstellt hast:
    • display_menu: Zeigt die Menüoptionen für den Benutzer an.
    • add_contact: Fügt einen neuen Kontakt zum Kontaktbuch hinzu.
    • view_contact: Zeigt Details für einen bestimmten Kontakt an.
    • edit_contact: Aktualisiert die Informationen eines bestehenden Kontakts.
    • delete_contact: Entfernt einen Kontakt aus dem Kontaktbuch.
    • list_all_contacts: Zeigt alle Kontakte im Kontaktbuch an.
  2. Erstelle ein Dictionary namens contact_book, um die Kontakte zu speichern.
  3. Schreibe eine Schleife, die:
    • Das Menü mit display_menu anzeigt.
    • Benutzereingaben für die Menüauswahl entgegennimmt.
    • Die entsprechende Funktion basierend auf der Auswahl des Benutzers aufruft.
    • Invalid choice. Please try again. ausgibt, wenn der Benutzer eine Auswahl trifft, die keine der gültigen Menüoptionen ist.
    • Fortfährt, bis der Benutzer die Option zum Beenden des Programms wählt.

Erwartetes Verhalten:

Wenn du das Programm ausführst, sollte es:

  1. Ein Menü mit Optionen anzeigen, aus denen der Benutzer wählen kann.
  2. Dem Benutzer ermöglichen, mit dem Kontaktbuch zu interagieren, indem die entsprechende Funktion aufgerufen wird.
  3. Invalid choice. Please try again. ausgeben, wenn der Benutzer eine nicht erkannte Option eingibt.
  4. Das Programm sauber beenden, wenn der Benutzer die Option "Exit" wählt.

Dieser letzte Schritt kombiniert all das Wissen, das du erworben hast, zu einer voll funktionsfähigen Kontaktbuch-Anwendung. Viel Spaß dabei, zu sehen, wie deine harte Arbeit Früchte trägt! 🎉

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

def delete_contact(contact_book):
    name = input()
    if name in contact_book:
        del contact_book[name]
        print("Contact deleted successfully!")
    else:
        print("Contact not found!")

def list_all_contacts(contact_book):
    if not contact_book:
        print("No contacts available.")
    else:
        for name, details in contact_book.items():
            print(f"Name: {name}")
            print(f"Phone: {details['phone']}")
            print(f"Email: {details['email']}")
            print(f"Address: {details['address']}")
            print()  # Blank line between contacts for readability

Alle Lektionen in Logic & Flow