전체 목록
Coddy Python 여정의 Logic & Flow 섹션에 포함된 레슨 — 78개 중 22번째.
챌린지
쉬움다음 단계는 list_all_contacts 함수를 만드는 것입니다. 이 함수는 사용자가 연락처에 저장된 모든 연락처와 세부 정보를 볼 수 있게 해줍니다.
작업 내용:
contact_book(딕셔너리)이라는 하나의 인자를 받는list_all_contacts라는 이름의 함수를 만듭니다.contact_book이 비어 있는지 확인합니다:- 비어 있다면,
"No contacts available."를 출력합니다.
- 비어 있다면,
- 비어 있지 않다면:
- 딕셔너리의 각 연락처를 반복하며 이름(name), 전화번호(phone), 이메일(email), 주소(address)를 읽기 쉬운 형식으로 출력합니다.
예상 동작:
다음과 같은 contact_book이 있을 때:
{
"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"}
}
출력 결과는 다음과 같아야 합니다:
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직접 해보기
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!")