Week 22
Source Code
-- define main function
function main()
print("Main Function")
local done = false
local choice = nil
while not done do
print("Menu")
print("E1 - Example 1")
print("Q - Quit")
io.write("Choice: ")
choice = io.read()
if choice == "E1" then
-- call Example 1 Function
example1()
elseif choice == "E2" then
-- call Example 2 Function
example2()
elseif choice == "E3" then
-- call Example 3 Function
example3()
end
end
end
-- define Example 1 Function
function example1()
-- empty list of friends
-- add friends
-- call printDictionary
-- delete a friend
-- call printDictionary
-- access a key's value (two versions)
end
-- define printDictionary Function
function printDictionary(d)
-- Using a loop to print the values
end
-- define Example 2 Function
function example2()
-- Accessing values from the dictionary using dot notation
end
-- call main function
main()
dictionary_adventure.lua
function main()
local inventory = {} -- Initialize an empty inventory
print("Your adventure begins!")
-- Display initial inventory (should be empty at the start)
displayInventory(inventory)
io.write("Would you like to visit the Forest? ")
local choice = io.read()
if choice == "Yes" then
encounterForest(inventory)
else
print("You went another way")
end
-- Display inventory after the forest encounter
displayInventory(inventory)
-- Continue your story..
end
-- Function to display the current inventory contents
-- It iterates over the inventory dictionary and prints each item with its quantity
function displayInventory(inventory)
end
-- Function to add an item to the inventory
-- If the item already exists, it increments its quantity; otherwise, it adds the item with quantity 1
function addItem(inventory, item)
end
-- Function to check if an item is present in the inventory
-- Returns true if the item exists and its quantity is greater than 0, false otherwise
function hasItem(inventory, item)
end
-- Adventure scenarios
-- A scenario function representing an encounter in a forest
-- Adds an item to the player's inventory upon encountering this scenario
function encounterForest(inventory)
print("You visited the forest..!")
end
-- call to main function
main()
restaurant.lua
-- define main function
function main()
print("Main Function")
local myOrder = {}
local done = false
local choice = nil
while not done do
print("Menu")
print("I - Restaurant Information")
print("A - Appetizers")
print("E - Entrees")
print("B - Beverages")
print("M - Modify Order")
print("P - Place Order")
print("Q - Quit")
io.write("Choice: ")
choice = io.read()
if choice == "Q" then
print("Quit!")
done = true
-- more choices
else
print("Invalid Choice")
end
end
end
-- define information function
-- define appetizers function
-- define entrees function
-- define desserts function
-- define beverages function
-- define modifyOrder function
-- define placeOrder function
-- call main function
main()
Last updated