연락처 삭제
Coddy Python 여정의 Logic & Flow 섹션에 포함된 레슨 — 78개 중 21번째.
챌린지
쉬움다음 단계는 delete_contact 함수를 만드는 것입니다. 이 함수는 사용자가 연락처 앱에서 특정 연락처를 삭제할 수 있게 해줍니다.
작업 내용:
contact_book(딕셔너리)이라는 하나의 인자를 받는delete_contact라는 이름의 함수를 생성하세요.- 삭제하려는 연락처의 이름을 입력받으세요.
contact_book에 해당 이름이 존재하는지 확인하세요:- 존재한다면, 딕셔너리에서 해당 연락처를 제거하세요.
"Contact deleted successfully!"를 출력하세요.
- 연락처가 존재하지 않는다면,
"Contact not found!"를 출력하세요.
직접 해보기
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!")