4 posts
Posted 09 August 2013 - 09:43 AM
Title: My if statement isn't working.
I'm trying to make a program that when I type test 10 it says hi and when I use another number it says fail but it always returns fail even if i type test 10.
local arg=...
if arg == 10 then
print("hi")
else
print("fail")
end
1190 posts
Location
RHIT
Posted 09 August 2013 - 09:45 AM
Split into new topic.
The reason for this is that arguments are passed into programs as strings. So in order for that comparison to work, you have to use tonumber(arg) and convert it into an actual number.
1852 posts
Location
Sweden
Posted 09 August 2013 - 10:10 AM
Split into new topic.
The reason for this is that arguments are passed into programs as strings. So in order for that comparison to work, you have to use tonumber(arg) and convert it into an actual number.
Or just make 10 a string
-- Changing 10 to a string
local arg= ...
if arg == "10" then -- The arg is a string and should be compared with a string
print("hi")
else
print("fail")
end
--Changing arg to a number
local arg= tonumber(...)
if arg == 10 then
print("hi")
else
print("fail")
end
1190 posts
Location
RHIT
Posted 09 August 2013 - 10:13 AM
Split into new topic.
The reason for this is that arguments are passed into programs as strings. So in order for that comparison to work, you have to use tonumber(arg) and convert it into an actual number.
Or just make 10 a string
-- Changing 10 to a string
local arg= ...
if arg == "10" then -- The arg is a string and should be compared with a string
print("hi")
else
print("fail")
end
--Changing arg to a number
local arg= tonumber(...)
if arg == 10 then
print("hi")
else
print("fail")
end
Sure. or use tonumber so you can do useful number operations. The only reason to use a string to compare is if you want to save yourself about five or six characters.