2016-12-06 88 views
1

嗨,所以我剛剛安裝了Lua,我一直在玩它。當我運行一個應該計算一個整數是偶數還是奇數的程序時,它會向我發出一個錯誤。Lua:「嘗試索引nill值」

計劃:

function is_even(n) 
    if bit32.band(n,1) == 0 then 
    print('Even') 
    else 
    print('Odd') 
    end 
end 

這是我收到的錯誤:

stdin:2: attempt to index a nil value (global 'bit32') 
stack traceback: 
     stdin:2: in function 'is_even' 
     (...tail calls...) 
     [C]: in ? 

我到底錯在這裏做什麼?這個程序應該在Lua 5.2+上工作,我目前已經安裝了Lua 5.3.3。

+0

你是怎麼調用這個程序的?它似乎在Lua下工作:演示在線https://www.lua.org/cgi-bin/demo – ciriarte

+1

'如果n%2 == 0那麼'是更通用的解決方案。 –

+0

上面的程序正是我在cmd中運行的程序。和n%2 == 0的作品,但我想使用新的5.2 bit32功能。 – Laurens

回答

0

從Lua 5.3中刪除了bit32庫,因爲它現在支持bitwise operators

+0

'bit32'仍然在Lua 5.3中,並且如果定義了'LUA_COMPAT_5_2',則編譯它,它位於lua.org的Makefile中。 – lhf

相關問題