Displaying the Inventory
Part of the Fundamentals section of Coddy's Lua journey — lesson 79 of 90.
Challenge
EasyBuilding on your inventory with one potion, create a display system to show all available potions in your shop. First, add a second potion to your inventory by creating a dictionary-style table named manaPotion with the following properties:
namewith the value"Mana Potion"pricewith the value12stockwith the value5
Use table.insert() to add this manaPotion to your inventory table.
Next, create a display system using a numeric for loop to iterate through your inventory table. For each potion in the inventory, print the potion's name and price in the format: "[name]: [price] gold". Use string concatenation to combine the potion's name and price with the descriptive text.
Try it yourself
-- Create empty inventory table
local inventory = {}
-- Create health potion table
local healthPotion = {
name = "Health Potion",
price = 15,
stock = 8
}
-- Add health potion to inventory
table.insert(inventory, healthPotion)
-- Print the length of inventory
print(#inventory)All lessons in Fundamentals
4Operators 2 Relational & Logic
Equality OperatorsRelational OperatorsThe 'and' OperatorThe 'or' OperatorThe 'not' OperatorShort-Circuit EvaluationTruthy and Falsy ValuesRecap - Simple Logic7Basic Conditional Logic
The if-then StatementThe if-then-else StatementThe elseif StatementRecap - Treasure Chest10Tables Part 1: List-Style
What is a Table?Creating a TableAccessing Elements by IndexModifying Elements by IndexThe Length Operator '#'table.insert()table.remove()Recap - Manage Party Members13Project: Simple Potion Shop
Project SetupAdding a Potion2Variables and Data Types
What is a Variable?NumbersStringsBooleansThe Value 'nil'The type() FunctionNaming ConventionsRecap - Character Profile5Basic Output
Printing LiteralsPrinting VariablesPrinting Multiple ValuesCombining Strings & VariablesThe tostring() FunctionInputCastRecap - Status ReportRecap - Till 1208String Manipulation Basics
string.len()string.upper & string.lowerstring.sub()string.rep()string.find()Recap - Format Username3Operators 1 Arithmetic & Conc
Arithmetic OperatorsModulo OperatorExponentiation OperatorString ConcatenationOperator PrecedenceRecap - Simple Calculations6Project: Character Stats Disp
Welcome MessageDeclare Character Stats9Functions Basics
Declaring a FunctionCalling a FunctionFunctions with ParametersFunctions with Multiple ParamsThe 'return' StatementRecap - Area Calculator