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

How do I do two or more "if input == then"?

Started by Kadecamz, 15 August 2012 - 05:23 AM
Kadecamz #1
Posted 15 August 2012 - 07:23 AM
The first if and then second elseif does not work for me.
Please help.
KaoS #2
Posted 15 August 2012 - 07:33 AM
I am not sure what you are asking but will answer as best I can:

if you want to make 2 if statements just use and

if variable1=="string1" and variable2=="string2" then end

if you want to nest if statements then that works fine as well

if var1=="str1" then
if var2=="str2" then
  --successcode
end
end

however be very careful about the 'end's as if you want to do an else statement for the first if (var1=="str1") then you must close the second one first

if var1=="str1" then
if var2=="str2" then
  --successcode
end
else
--othercode
end
Kadecamz #3
Posted 15 August 2012 - 07:40 AM
I don't get what you said, Kaos.
I dunno what variables are.
immibis #4
Posted 15 August 2012 - 07:41 AM
If you want to check whether both are equal:

if myVariable == "a" and myOtherVariable == "b" then
  print("myVariable was a and myOtherVariable was b!")
end

If you want to check if one (or both) are equal:

if myVariable == "a" and myOtherVariable == "b" then
  print("myVariable was a or myOtherVariable was b or both!")
end
Luanub #5
Posted 15 August 2012 - 07:42 AM
If you are wanting to check one input with one if statement doing multiple checks doing something like.

if var == "something" then
  --do something
elseif var == "somethingelse" then
  --do something else
else
  --do this.
end
Kadecamz #6
Posted 15 August 2012 - 07:43 AM
uhg.
I want it to be able to work with more than one if input == statements so that the program I made can have multiple options.
Luanub #7
Posted 15 August 2012 - 07:48 AM
input is a var. Replace var in any of the above with input and it will work the same.

So

local input = read()
if input == "password1" then
--do password1 stuffs
elseif input == "password2" then
-- do password2 stuffs
end

Variables are very basic. I suggest if you are this new to programming to you work through a couple of the tutorials that are available.

You can also do and's and or's in your if statements as well

if input == "password1" or input == "password2" then
-- do password stuff
end


if input1 == "user1" and input2 == "password1" then
--do stuff
elseif input1 == "user2" and input2 == "password2" then
--do other stuff
end
jay5476 #8
Posted 15 August 2012 - 10:19 AM
please give a code to see where the error is plz
Kadecamz #9
Posted 15 August 2012 - 09:28 PM
I'm not new.
I probably knew what they were, just not that they were called variables

Here is some of my code that I made just now.


local code = "1337"
term.clear()
sleep(0.1)
term.setCursorPos(1,1)
print "Enter the code! Be sure the silo is powered."
write "CODE: "
input = read()
if input == "1337" then
function success()
write "Accessing mainframe"
sleep (2)
write "."
sleep (2)
write "."
sleep(1)
write "."
sleep (1)
term.clear()
sleep(0.1)
term.setCursorPos(1,1)
print "Success! The missiles are firing in"
write "3"
sleep(1)
term.clearLine()
term.setCursorPos(1,2)
write "2"
sleep(1)
term.clearLine()
term.setCursorPos(1,2)
write "1"
sleep(1)
print ".....FIRING!!!!"
sleep(0.3)
rs.setOutput ("right", true)
print "Action complete"
sleep(2)
os.shutdown()
end
function fail()
term.clear()
term.setCursorPos(1,1)
sleep(0.1)
write "Failure has accoured."
sleep(3)
os.shutdown()
end
chance = math.random (1,5)
if chance == 1 then
success()
elseif chance == 2 then
success()
elseif chance == 3 then
success()
elseif chance == 4 then
success()
elseif chance == 5 then
fail()
end
elseif input == "modify" then
print "You may now modify the mainframe"
sleep(1)
elseif input == "abort" then
write "Aborting Files"
sleep(1)
write "."
sleep(1)
write "."
sleep(1)
write "."
sleep(0.5)
term.setCursorPos(1,5)
print "Successfully Aborted!"
sleep(3)
os.shutdown()
else
write "Accessing mainframe"
sleep(1)
write "."
sleep(1)
write "."
sleep(1)
write "."
term.clearLine()
term.setCursorPos(1,4)
print "Wrong code!"
sleep(3)
os.shutdown()
end

EDIT:Luan, how would it detect what was input2?
Luanub #10
Posted 15 August 2012 - 11:03 PM
One issue I see is that you have your functions listed in your if statements. With Lua its best to put all of your functions at the top of the code then the functional portions of the code at the bottom that way Lua has loaded all of the functions before actually trying to do something.

Example

--declare vars here
local code = "1337"

--do your functions
local function clear()
  term.clear()
  term.setCursorPos(1,3)
end

--then the code
clear()
write("Enter the code")
local input = read()  -- always good practice to make the vars local
if input == code then
  -- do stuff
elseif input == "something else" then
-- do that
end

As far as detecting what input2 is. Where do you want to get the value of input2? User input? From another function?

local passwd = "password"
local user = "username"

write("Enter Username")
local input = read()
write("Enter Password")
local input2 = read("*")
if input == user and input2 == passwd then
  print("Correct info entered")
else
  print("Invalid Password")
end


local input2 = rs.getInput("right")
if input2 then
  rs.setOutput("left", true)
end