Menu
CoddyTech

모든 기능 함께 구현하기

Coddy GDScript 여정의 논리와 흐름 섹션에 포함된 레슨. 78개 중 30번째.

challenge icon

챌린지

쉬움

마지막 단계에서는 루프를 정리하고 잘못된 선택을 처리합니다. 이전 단계에서 작성한 내용은 모두 그대로 유지하세요.

  1. 루프의 if / elif 체인을 match choice: 문으로 바꾸고, "1"부터 "6"까지 각 선택지마다 패턴을 하나씩 추가하세요. 패턴 안의 break는 여전히 while 루프를 빠져나갑니다.
  2. 그 외의 모든 선택지를 처리할 와일드카드 패턴 _:을 추가하세요. 다음을 출력합니다.
Invalid choice. Please try again.

메시지를 표시한 후에는 이전과 마찬가지로 루프가 메뉴를 표시하고 다음 선택지를 읽습니다.

직접 해보기

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 == "3":
			edit_contact(contact_book)
		elif choice == "4":
			delete_contact(contact_book)
		elif choice == "5":
			list_all_contacts(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!")

func edit_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]
		var phone := OS.read_string_from_stdin()
		var email := OS.read_string_from_stdin()
		var address := OS.read_string_from_stdin()
		if phone != "":
			contact["phone"] = phone
		if email != "":
			contact["email"] = email
		if address != "":
			contact["address"] = address
		print("Contact updated successfully!")
	else:
		print("Contact not found!")

func delete_contact(contact_book: Dictionary) -> void:
	var contact_name := OS.read_string_from_stdin()
	if contact_book.has(contact_name):
		contact_book.erase(contact_name)
		print("Contact deleted successfully!")
	else:
		print("Contact not found!")

func list_all_contacts(contact_book: Dictionary) -> void:
	if contact_book.is_empty():
		print("No contacts available.")
	else:
		for contact_name in contact_book:
			var contact: Dictionary = contact_book[contact_name]
			print("Name: ", contact_name)
			print("Phone: ", contact["phone"])
			print("Email: ", contact["email"])
			print("Address: ", contact["address"])
			print()

논리와 흐름의 모든 레슨

직접 연습해 보세요: 온라인 GDScript 컴파일러