Menu
CoddyTech

인벤토리 표시하기

Coddy Lua 여정의 기초 섹션에 포함된 레슨. 90개 중 79번째.

challenge icon

챌린지

쉬움

물약 하나가 있는 인벤토리를 바탕으로, 상점에서 사용할 수 있는 모든 물약을 보여 주는 표시 시스템을 만드세요. 먼저 manaPotion이라는 딕셔너리 스타일의 테이블을 만들고 다음 속성을 설정하여 인벤토리에 두 번째 물약을 추가하세요.

  • name의 값은 "Mana Potion"
  • price의 값은 12
  • stock의 값은 5

table.insert()를 사용하여 이 manaPotion을 inventory 테이블에 추가하세요.

다음으로 숫자형 for 루프를 사용해 inventory 테이블을 순회하는 표시 시스템을 만드세요. 인벤토리의 각 물약에 대해 물약의 이름과 가격을 다음 형식으로 출력하세요: "[name]: [price] gold". 문자열 연결을 사용하여 물약의 name과 price를 설명 문구와 결합하세요.

이전 단계에서 작성한 print(#inventory) 줄을 제거하여 물약 목록만 출력되도록 하세요.

직접 해보기

-- 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)

기초의 모든 레슨

직접 연습해 보세요: 온라인 Lua 컴파일러