Menu
Coddy logo textTech

Selling a Potion

Part of the Fundamentals section of Coddy's Lua journey — lesson 82 of 90.

challenge icon

Challenge

Easy

Complete your potion shop project by implementing a sales transaction system. Building on your search functionality, create a system that processes a potion sale by reducing the stock quantity and providing transaction confirmation.

The following input will be provided:

  • A string containing the name of the potion to sell

Use a numeric for loop to iterate through your inventory table to find the specified potion. When you locate the matching potion using the equality operator ==, decrease its stock value by 1 to simulate the sale.

After updating the stock, print a confirmation message showing the potion name and its new stock level in the format:

Sold 1 [potion name]
Remaining stock: [new stock count]

Use string concatenation to combine the descriptive text with the potion's name and updated stock quantity. After processing the sale, use the break statement to exit the loop.

Try it yourself

-- Inventory table with potion data
inventory = {
    {name = "Health Potion", price = 15, stock = 8},
    {name = "Mana Potion", price = 12, stock = 5},
    {name = "Strength Potion", price = 25, stock = 3},
    {name = "Speed Potion", price = 18, stock = 2}
}

-- Read the potion name to search for
search_name = io.read()

-- Use a numeric for loop to search through the inventory
for i = 1, #inventory do
    if inventory[i].name == search_name then
        print("Found: " .. inventory[i].name)
        print("Price: " .. inventory[i].price .. " gold")
        print("Stock: " .. inventory[i].stock .. " units")
        break
    end
end

All lessons in Fundamentals