-- 2.宣告一個function,輸入一個正整數X,回傳一個高度為 X 的金字塔,若輸入非正整數要報錯
-- ex:
-- input : 3
-- return :
--   *  
--  * * 
-- * * * 
-- input : "a"
-- return : "Error, input value must be a positive integer"

function EX_2(x)
    if(type(x) == "number" and x > 0 and x%1==0)
    then 
        local i = 1
        while(x >= i)
        do
            print(string.rep(" ", x+1-i)..string.rep("* ", i)..string.rep(" ", x-i))
            i = i + 1
        end
    else
        print("Error, input value must be a positive integer.")
    end
end

EX_2(15)

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: