연락처 추가하기
Coddy Lua 여정의 Logic & Flow 섹션에 포함된 레슨 — 54개 중 43번째.
챌린지
쉬움연락처 관리 애플리케이션을 위한 연락처 추가 기능을 구현하세요. 사용자가 add를 입력하면, 프로그램은 연락처 세부 정보를 요청하고 이를 contacts 리스트에 저장해야 합니다.
이전의 메인 루프를 바탕으로, add 명령어를 처리하는 기능을 추가하세요. 이 명령어가 입력되면 사용자에게 이름과 전화번호를 묻고, 이 상세 정보로 연락처 테이블을 생성한 뒤 table.insert()를 사용하여 contacts 리스트에 추가합니다.
요구 사항:
- 이전 챌린지의 모든 코드(설정, 메뉴 표시, 메인 루프)를 유지하세요.
- 사용자가
add를 입력하면Enter name:을 출력합니다. io.read()를 사용하여 이름을 읽습니다.Enter phone number:를 출력합니다.io.read()를 사용하여 전화번호를 읽습니다.name과phone을 키로 가지는 딕셔너리 스타일의 테이블을 생성합니다.table.insert()를 사용하여 이 연락처 테이블을contacts리스트에 삽입합니다.Contact added successfully!를 출력합니다.quit명령어는 이전과 동일하게 작동해야 합니다.- 다른 명령어들은 여전히
Command not yet implemented를 출력해야 합니다.
상호작용 예시:
Welcome to Contact List Manager!
Available Commands:
add - Add a new contact
list - Display all contacts
search - Search for a contact
delete - Delete a contact
quit - Exit the program
Enter command:
add
Enter name:
Alice
Enter phone number:
555-1234
Contact added successfully!
Enter command:
add
Enter name:
Bob
Enter phone number:
555-5678
Contact added successfully!
Enter command:
list
Command not yet implemented
Enter command:
quit
Goodbye!직접 해보기
-- Initialize the contacts table
local contacts = {}
-- Display welcome message and menu
print("Welcome to Contact List Manager!")
print()
print("Available Commands:")
print("add - Add a new contact")
print("list - Display all contacts")
print("search - Search for a contact")
print("delete - Delete a contact")
print("quit - Exit the program")
print()
-- Main program loop
while true do
print("Enter command:")
local command = io.read()
if command == "quit" then
print("Goodbye!")
break
else
print("Command not yet implemented")
end
print()
end