טיפול בקלט לא תקין
חלק מהיחידה לוגיקה וזרימת התוכנית במסלול ה-Lua של Coddy. שיעור 47 מתוך 54.
אתגר
קלהשלימו את אפליקציית רשימת אנשי הקשר שלכם על ידי הוספת טיפול מתאים בפקודות תפריט לא תקינות. כשהמשתמש מזין פקודה שאינה תואמת לאף אחת מהאפשרויות הזמינות, התוכנית אמורה להודיע לו ולהמשיך לפעול.
בהמשך לאתגר הקודם, הוסיפו תנאי else לטיפול בכל פקודה שאינה add, list, search, delete או quit. כך תשפרו את חוויית המשתמש באמצעות הצגת משוב ברור כאשר מוזנת פקודה שאינה מוכרת.
דרישות:
- השאירו את כל הקוד מהאתגר הקודם (הגדרה, הצגת התפריט, הלולאה הראשית והפונקציונליות להוספה, להצגה, לחיפוש ולמחיקה)
- הוסיפו תנאי
elseלטיפול בכל פקודה שאינה תואמת לאפשרויות התקינות - כאשר מוזנת פקודה לא תקינה, הדפיסו:
Invalid command. Please try again. - על התוכנית להמשיך לפעול לאחר הצגת ההודעה על הפקודה הלא תקינה
- כל הפקודות הקיימות (
add,list,search,delete,quit) צריכות להמשיך לפעול בדיוק כמו קודם
אינטראקציה לדוגמה:
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!נסו בעצמכם
-- 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כל השיעורים ביחידה לוגיקה וזרימת התוכנית
1איטרציה מתקדמת בטבלאות
איטרציה באמצעות pairs()איטרציה באמצעות ipairs()pairs() לעומת ipairs()סיכום – גיליון דמות2פונקציות נוספות של ספריית הטבלאות
table.concat()בניית טבלה ו־unpack()table.sort()מיון מותאם אישית באמצעות פונקציותסיכום – לוח שיאים5פרויקט: מנוע להרפתקת טקסט
הגדרת הפרויקט: החדרקישור בין חדרים8פרויקט: רשימת אנשי קשר
הגדרת הפרויקטהלולאה הראשיתתרגלו בעצמכם: קומפיילר Lua אונליין