盘点一下lua脚本和python的区别(基础)

时间:2022-07-28
本文章向大家介绍盘点一下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