연락처 삭제
Coddy Python 여정의 논리와 흐름 섹션에 포함된 레슨. 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!")논리와 흐름의 모든 레슨
직접 연습해 보세요: 온라인 Python 컴파일러