Gérer les entrées invalides
Fait partie de la section Logique et flux du Journey Lua de Coddy. Leçon 47 sur 54.
Défi
FacileTermine ton application de liste de contacts en ajoutant une gestion appropriée des 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 t’appuyant sur le défi précédent, ajoute 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 :
- Conserve tout le code du défi précédent (configuration, affichage du menu, boucle principale, fonctionnalités d’ajout, de liste, de recherche et de suppression)
- Ajoute une condition
elsepour gérer toute commande qui ne correspond pas aux options valides - Lorsqu’une commande invalide est saisie, affiche :
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 continuer à 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
endToutes les leçons de Logique et flux
1Itération avancée des tables
Itérer avec pairs()Itérer avec ipairs()pairs() contre ipairs()Récapitulatif - Fiche de personnage2Fonctions supplémentaires de la bibliothèque table
table.concat()construction de tables et unpack()table.sort()Tri personnalisé avec des fonctionsRécapitulatif - Tableau des meilleurs scores5Projet : moteur d’aventure textuelle
Configuration du projet : la pièceRelier les pièces8Projet : Liste de contacts
Configuration du projetLa boucle principale3Concepts avancés des fonctions
Retourner plusieurs valeursFonctions variadiques (...)Les fonctions comme valeurs de première classeFonctions anonymesQu’est-ce qu’une fermeture ?Récapitulatif - Gestionnaire d’événements simpleEntraînez-vous par vous-même : Compilateur Lua en ligne