Menu
Coddy logo textTech

Wyświetl wszystkie

Część sekcji Logika i przepływ programu ścieżki Python w Coddy. Lekcja 22 z 78.

challenge icon

Wyzwanie

Łatwy

Następnym krokiem jest utworzenie funkcji list_all_contacts. Ta funkcja umożliwi użytkownikom wyświetlenie wszystkich kontaktów zapisanych w książce kontaktów wraz z ich szczegółami.


Twoje zadanie:

  1. Utwórz funkcję o nazwie list_all_contacts, która przyjmuje jeden argument: contact_book (słownik).
  2. Sprawdź, czy contact_book jest pusty:
    • Jeśli jest pusty, wypisz: "No contacts available.".
  3. Jeśli nie jest pusty:
    • Przejdź przez każdy kontakt w słowniku i wypisz jego imię i nazwisko, numer telefonu, adres e-mail oraz adres w czytelnym formacie.

Oczekiwane działanie:

Dla słownika contact_book zawierającego:

{
    "Alice": {"phone": "123-456-7890", "email": "alice@example.com", "address": "123 Main St"},
    "Bob": {"phone": "234-567-8901", "email": "bob@example.com", "address": "456 Oak Ave"}
}

Wynik powinien wyglądać następująco:

Name: Alice
Phone: 123-456-7890
Email: alice@example.com
Address: 123 Main St

Name: Bob
Phone: 234-567-8901
Email: bob@example.com
Address: 456 Oak Ave

Spróbuj swoich sił

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

Wszystkie lekcje w sekcji Logika i przepływ programu

Poćwicz samodzielnie: Kompilator Python online