This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
robrecht's profile picture

Lua comparing number with string

Started by robrecht, 13 January 2013 - 11:53 PM
robrecht #1
Posted 14 January 2013 - 12:53 AM
I want the user to input a number, store it into a variable and then compare it. But the input seems to be a string i think! I'm a bit of a beginner with lua but i'm getting better and i can't work this out! This is what i got yet.

print ("Enter the number")
write ("number: ")
for i = 1, 1000, 1 do
a = read()
if a < 0 then
print ("whatever")
elseif a > 99 then
print ("That's too big!!!!")
else
break
end
end
Now if i run the script and input a number it says: attempt to compare number with string expected, got number.
so how do i make the input variable a number instead of a string or something? I hope i explained correctly, Thanks.
theoriginalbit #2
Posted 14 January 2013 - 01:05 AM
Yes input is a string… Use the function tonumber to convert a string to a number…
remiX #3
Posted 14 January 2013 - 01:05 AM
tonumber the variable 'a'

a=tonumber(read())
robrecht #4
Posted 14 January 2013 - 01:22 AM
thanks guys!