Lua For Variable In Range

后端 未结 2 748
误落风尘
误落风尘 2021-01-17 19:27

I can\'t seem to get this to work. I come from Python, so I tried using the same syntax for the hell of it, but it unsurprisingly wouldn\'t work:

var = 4

fo         


        
相关标签:
2条回答
  • 2021-01-17 19:58

    You could write your range function easily enough:

    function range ( from , to )
        return function (_,last)
                if last >= to then return nil
                else return last+1
                end
            end , nil , from-1
    end
    
    0 讨论(0)
  • 2021-01-17 20:05

    If you want to test whether a value is in a range, use

    if var>=2 and var<=20 then
       print ("var is in range")
    end
    

    If you want a loop, use

    for var=2,20 do
       print(var)
    end
    
    0 讨论(0)
提交回复
热议问题