盤點一下lua腳本和python的區別(基礎)


一.縮進

lua腳本無縮進但是有end結尾

二.邏輯判斷

if false or nil then      
    print("至少有一個是 true")
else
    print("false 和 nil 都為 false")
end

if 0 then
    print("數字 0 是 true")
else
    print("數字 0 為 false")
end

三.算術操作

#在對一個數字字符串上進行算術操作時,Lua 會嘗試將這個數字字符串轉成一個數字

四.獲取字符串的長度

#字符串變量

五.for循環

1.普通循環

for var=exp1,exp2,exp3 do  
    <執行體>  
end  

2.死循環

while( true )
do
   print("循環將永遠執行下去")
end

3.類似python中continue

方法一

for i = 10, 1, -1 do
  repeat
    if i == 5 then
      print("continue code here")
      break
    end
    print(i, "loop code here")
  until true
end

方法 二

for i=1, 3 do
    if i <= 2 then
        print(i, "yes continue")
        goto continue
    end
    print(i, " no continue")
    ::continue::
    print([[i'm end]])
end

六.方法的定義

optional_function_scope function function_name( argument1, argument2, argument3..., argumentn)
    function_body
    return result_params_comma_separated
end



optional_function_scope: 該參數是可選的制定函數是全局函數還是局部函數,未設置該參數默認為全局函數,如果你需要設置函數為局部函數需要使用關鍵字 local。

function_name: 指定函數名稱。

argument1, argument2, argument3..., argumentn: 函數參數,多個參數以逗號隔開,函數也可以不帶參數。

function_body: 函數體,函數中需要執行的代碼語句塊。

result_params_comma_separated: 函數返回值,Lua語言函數可以返回多個值,每個值以逗號隔開。

七.導入模塊

require "<模塊名>"

八.獨有的表結構

https://www.runoob.com/lua/lua-tables.html

九.類

https://www.runoob.com/lua/lua-object-oriented.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM