인벤토리 표시하기
Coddy Lua 여정의 Fundamentals 섹션에 포함된 레슨 — 90개 중 79번째.
챌린지
쉬움하나의 포션이 들어있는 인벤토리를 바탕으로, 상점에서 판매 가능한 모든 포션을 보여주는 표시 시스템을 만드세요. 먼저, 다음과 같은 속성을 가진 manaPotion이라는 이름의 딕셔너리 스타일 테이블을 생성하여 인벤토리에 두 번째 포션을 추가하세요:
name값은"Mana Potion"price값은12stock값은5
table.insert()를 사용하여 이 manaPotion을 inventory 테이블에 추가하세요.
다음으로, 숫자형 for 루프를 사용하여 inventory 테이블을 반복하는 표시 시스템을 만드세요. 인벤토리의 각 포션에 대해, 포션의 이름과 가격을 "[name]: [price] gold" 형식으로 출력하세요. 문자열 연결(string concatenation)을 사용하여 포션의 name 및 price를 설명 텍스트와 결합하세요.
직접 해보기
-- 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)