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

[Lua] [Question] Program is ignoring if-statement when it's not supposed to.

Started by rladngus, 19 October 2012 - 01:51 AM
rladngus #1
Posted 19 October 2012 - 03:51 AM
I have the following simple program made in my computercraft computer:

term.write("PASSCODE: ")
pass = io.read()
print(pass)

if pass == 5148 then
[indent] print("successful")
else
[indent] print("failed")
end

Theoretically, if I punch in the number 5148 when I run this program, I'm supposed to get the word "successful" printed on my screen. But I'm always getting "failed". Doesn't matter what I type in. Any random junk, and it's always "failed," even when I'm telling the program to specifically check if the number input from the user is the same as 5148. What's going on?

I tried this with return statements too. As in:

if pass == 5148 then
[indent] return True
else
[indent] return False
end

not sure if that syntax is allowed in Lua. Anyone knows?
Kingdaro #2
Posted 19 October 2012 - 04:03 AM
io.read, or just read(), returns a string (text), regardless of whether you entered a number or not. It will never equal 5148 because 5148 isn't a string, it's a number.

You would either have to convert your pass variable to a number with tonumber(), or put quotes around 5148.

Also, that second example you've given wouldn't work, due to the fact that it's not actually returning to anything because it isn't inside a function (unless I'm mistaken and scripts are loaded inside a function.) Plus, true and false have to be lowercase, otherwise Lua mistakes them as variables and returns nil since they haven't been set.
JoshhT #3
Posted 19 October 2012 - 04:39 AM
io.read, or just read(), returns a string (text), regardless of whether you entered a number or not. It will never equal 5148 because 5148 isn't a string, it's a number.

You would either have to convert your pass variable to a number with tonumber(), or put quotes around 5148.

Also, that second example you've given wouldn't work, due to the fact that it's not actually returning to anything because it isn't inside a function (unless I'm mistaken and scripts are loaded inside a function.) Plus, true and false have to be lowercase, otherwise Lua mistakes them as variables and returns nil since they haven't been set.

Couldn't have said it any better myself friend.
rladngus #4
Posted 19 October 2012 - 04:48 AM
io.read, or just read(), returns a string (text), regardless of whether you entered a number or not. It will never equal 5148 because 5148 isn't a string, it's a number.

You would either have to convert your pass variable to a number with tonumber(), or put quotes around 5148.

Also, that second example you've given wouldn't work, due to the fact that it's not actually returning to anything because it isn't inside a function (unless I'm mistaken and scripts are loaded inside a function.) Plus, true and false have to be lowercase, otherwise Lua mistakes them as variables and returns nil since they haven't been set.

I knew it. I knew it was a type issue. But I couldn't find a way to check for types. Not even Google was of any help. Thanks.
Lyqyd #5
Posted 19 October 2012 - 05:19 AM
For future reference, you can find the type of a variable with type(var), which returns a string, usually "boolean", "string", "number", or "table". You can tostring() things to attempt to make them into strings and tonumber() them to attempt to turn them into numbers. A second argument to tonumber() specifies the base of the starting number, so tonumber(var, 16) will convert a hexadecimal number to a decimal number.
remiX #6
Posted 19 October 2012 - 06:27 AM
You can either do
pass = tonumber(io.read())
or put quotes around the numbers in the if statements