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

Door Lock Program

Started by NCIS_LEGEND, 04 April 2015 - 04:18 PM
NCIS_LEGEND #1
Posted 04 April 2015 - 06:18 PM
So I am trying to run this code and it is doing the last if even if the code is right. Any ideas how to fix it.


while true do
term.clear()
term.setCursorPos(1, 1)
print("This is the Liquid Storage Room\nIf you want to gain access,\nPlease Enter the Password:")
input = read("*")
if input == "abcd1234" then
  redstone.setOutput("right", true)
  term.setTextColor(colors.green)
  print("Password Accepted")
  sleep(3)
  term.setTextColor(colors.white)
  redstone.setOutput("right", false)
end

if input == "masterOpen" then
  redstone.setOutput("right", true)
  term.setTextColor(colors.green)
  print("masterOpen Activated")
  term.setTextColor(colors.white)
  sleep(3)
end

if input == "masterClose" then
  redstone.setOutput("right", false)
  term.setTextColor(colors.green)
  print("masterClose Activated")
  term.setTextColor(colors.white)
  sleep(3)
end

if input ~= "masterClose" or input~= "masterOpen" or input ~= "abcd1234" then
  term.setTextColor(colors.red)
  print("Password Incorrect. Please try again.")
  term.setTextColor(colors.white)
  sleep(3)
end

sleep(2)
end

Here is a picture of the error.
http://www.upimg.me/...377330ac089dfa6
Lyqyd #2
Posted 04 April 2015 - 06:40 PM
Moved to Ask a Pro.

Your last conditional is using ors instead of ands. If the input variable is any of the strings you've already checked for, the other two conditionals will still return true (if the input is "masterClose", the checks against "masterOpen" and "abcd1234" will both be true), so it will always take that path. Using ands there means that it will only take that option if it is not any of the three options above.
HPWebcamAble #3
Posted 04 April 2015 - 07:15 PM
You can use else and elseif statments to streamline your code (And fix the bug)


while true do

  term.clear()
  term.setCursorPos(1,1)
  print("This is the Liquid Storage Room\nIf you want to gain access,\nPlease Enter the Password:")
  input = read("*")

  if input == "abcd1234" then --# Is the password abcd1234?
    --# Do stuff
  elseif input == "masterOpen" then --# Wasn't abcd1234, is it masterOpen?
    --# Do stuff
  elseif input == "masterClose" then --# Wasn't abcd1234 or masterOpen, is it masterClose?
    --# Do stuff
  else --# Wasn't any of the ones we are checking for, and must be wrong
    print("Wrong!")
  end

  sleep(2)
end