The Main Loop
Part of the Logic & Flow section of Coddy's Lua journey — lesson 42 of 54.
Challenge
EasyExpand your contact list application by implementing the main program loop that keeps the application running until the user decides to quit.
Building on the previous setup, create a while loop that continuously displays the menu and prompts the user for input. The loop should continue running until the user enters quit, at which point the program should exit gracefully.
Requirements:
- Keep all code from the previous challenge (the
contactstable and initial menu display) - After displaying the menu, print a blank line
- Create a
while trueloop for the main application loop - Inside the loop, print the prompt:
Enter command: - Read the user's input using
io.read() - If the input is
quit, printGoodbye!and break the loop - For any other input, print
Command not yet implemented - After processing the command, print a blank line before the next prompt
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
Command not yet implemented
Enter command:
list
Command not yet implemented
Enter command:
quit
Goodbye!Try it yourself
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")All 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