Menu
Coddy logo textTech

Gérer les entrées invalides

Fait partie de la section Logic & Flow du Journey Lua de Coddy — leçon 47 sur 54.

challenge icon

Défi

Facile

Complétez votre application de liste de contacts en ajoutant une gestion appropriée pour les commandes de menu invalides. Lorsque l'utilisateur saisit une commande qui ne correspond à aucune des options disponibles, le programme doit l'en informer et continuer à s'exécuter.

En vous basant sur le défi précédent, ajoutez une condition else pour gérer toute commande qui n'est pas add, list, search, delete ou quit. Cela améliore l'expérience utilisateur en fournissant un retour clair lorsqu'une commande non reconnue est saisie.

Exigences :

  • Conservez tout le code du défi précédent (configuration, affichage du menu, boucle principale, fonctionnalités add, list, search et delete)
  • Ajoutez une condition else pour gérer toute commande qui ne correspond pas aux options valides
  • Lorsqu'une commande invalide est saisie, affichez : Invalid command. Please try again.
  • Le programme doit continuer à s'exécuter après l'affichage du message de commande invalide
  • Toutes les commandes existantes (add, list, search, delete, quit) doivent toujours fonctionner exactement comme avant

Exemple d'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!

Essayez vous-même

-- 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
end

Toutes les leçons de Logic & Flow