연락처 수정
Coddy GDScript 여정의 논리와 흐름 섹션에 포함된 레슨. 78개 중 27번째.
챌린지
쉬움연락처를 수정하는 선택지 3을 처리하세요. 이전 단계에서 작성한 내용은 모두 그대로 유지하세요.
edit_contact(contact_book: Dictionary)를 작성하세요. 이 함수는 이름을 읽습니다.
- 이름이
contact_book에 없으면Contact not found!을 출력하고 다른 내용은 읽지 않습니다. - 그렇지 않으면 새 전화번호, 새 이메일, 새 주소 순서로 세 줄을 읽습니다. 빈 줄은 "현재 값 유지"를 의미합니다. 비어 있지 않은 필드를 업데이트한 다음
Contact updated successfully!을 출력하세요.
예를 들어, 연락처 목록에 Alice가 있고 선택지 3을 입력한 뒤의 입력이 다음과 같다면:
Alice
987-654-3210
456 Elm StAlice의 전화번호는 987-654-3210으로, 주소는 456 Elm St로 바뀌고 이메일은 그대로 유지됩니다.
루프에 "3"에 대한 elif 분기를 추가하여 edit_contact(contact_book)를 호출하세요.
직접 해보기
extends Node
func _ready():
var contact_book := {}
while true:
display_menu()
var choice := OS.read_string_from_stdin()
if choice == "1":
add_contact(contact_book)
elif choice == "2":
view_contact(contact_book)
elif choice == "6":
break
func display_menu() -> void:
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")
func add_contact(contact_book: Dictionary) -> void:
var contact_name := OS.read_string_from_stdin()
if contact_book.has(contact_name):
print("Contact already exists!")
return
var phone := OS.read_string_from_stdin()
var email := OS.read_string_from_stdin()
var address := OS.read_string_from_stdin()
contact_book[contact_name] = {"phone": phone, "email": email, "address": address}
print("Contact added successfully!")
func view_contact(contact_book: Dictionary) -> void:
var contact_name := OS.read_string_from_stdin()
if contact_book.has(contact_name):
var contact: Dictionary = contact_book[contact_name]
print("Name: ", contact_name)
print("Phone: ", contact["phone"])
print("Email: ", contact["email"])
print("Address: ", contact["address"])
else:
print("Contact not found!")
논리와 흐름의 모든 레슨
직접 연습해 보세요: 온라인 GDScript 컴파일러