Handling Invalid Input
Part of the Logic & Flow section of Coddy's Lua journey — lesson 47 of 54.
Challenge
EasyComplete your contact list application by adding proper handling for invalid menu commands. When the user enters a command that doesn't match any of the available options, the program should inform them and continue running.
Building on the previous challenge, add an else condition to handle any command that isn't add, list, search, delete, or quit. This improves the user experience by providing clear feedback when an unrecognized command is entered.
Requirements:
- Keep all code from the previous challenge (setup, menu display, main loop, add, list, search, and delete functionality)
- Add an
elsecondition to handle any command that doesn't match the valid options - When an invalid command is entered, print:
Invalid command. Please try again. - The program should continue running after displaying the invalid command message
- All existing commands (
add,list,search,delete,quit) should still work exactly as before
Example interaction:
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:
show
Invalid command. Please try again.
Enter command:
remove
Invalid command. Please try again.
Enter command:
list
Contact List:
1. Alice - 555-1234
Enter command:
help
Invalid command. Please try again.
Enter command:
quit
Goodbye!Try it yourself
-- Contact List Manager with Delete Functionality
local contacts = {}
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()
while true do
print("Enter command:")
local command = io.read()
if command == "add" then
print("Enter name:")
local name = io.read()
print("Enter phone number:")
local phone = io.read()
local contact = {name = name, phone = phone}
table.insert(contacts, contact)
print("Contact added successfully!")
print()
elseif command == "list" then
if #contacts == 0 then
print("No contacts available.")
else
print("Contact List:")
for i, contact in ipairs(contacts) do
print(i .. ". " .. contact.name .. " - " .. contact.phone)
end
end
print()
elseif command == "search" then
print("Enter name to search:")
local searchName = io.read()
local found = false
for i, contact in ipairs(contacts) do
if contact.name == searchName then
print("Found: " .. contact.name .. " - " .. contact.phone)
found = true
break
end
end
if not found then
print("Contact not found.")
end
print()
elseif command == "delete" then
print("Enter name to delete:")
local deleteName = io.read()
local found = false
for i, contact in ipairs(contacts) do
if contact.name == deleteName then
table.remove(contacts, i)
print("Contact deleted successfully!")
found = true
break
end
end
if not found then
print("Contact not found.")
end
print()
elseif command == "quit" then
print("Goodbye!")
break
end
endAll lessons in Logic & Flow
1Advanced Table Iteration
Iterating with pairs()Iterating with ipairs()pairs() vs. ipairs()Recap - Character Sheet2More Table Library Functions
table.concat()table construction & unpack()table.sort()Custom Sorting with FunctionsRecap - High Score Board5Project: Text Adventure Engine
Project Setup: The RoomLinking Rooms8Project: Contact List
Project SetupThe Main Loop