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

Problematic 'else' command

Started by Pedroc1999, 30 October 2012 - 09:05 PM
Pedroc1999 #1
Posted 30 October 2012 - 10:05 PM
i have made a program that works like a lever, but i want to add a comment like 'code incorrect' when there is no such code. I am new to the else command and this is my first implementation

could someone review my program and see any problems, it works as long as i dont add a 'else' command.


os.pullEvent = os.pullEventRaw -- Disable CTRL-T
while true do -- Code Written By  Pedroc1999
term.clear()
term.setCursorPos(1, 1)
print("Please Enter Codes")
input = read("*")
if input == "open" then
  redstone.setOutput("back", false)
  end
if input == "close" then
  redstone.setOutput("back", true)
  end
if input == "os" then
  term.clear()
  term.setCursorPos(1, 1)
  print("Returning to CraftOS 1.4")
  sleep(1)
  print(" ")
  print("  3")
  sleep(1)
  print("  2")
  sleep(1)
  print("  1")
  sleep(1)
  term.clear()
  term.setCursorPos(1, 1)
  error() -- Simply Terminates Program
  end
else
  print("Code Not Found")
  end
end
Orwell #2
Posted 30 October 2012 - 10:11 PM
when you have multiple conditionals like in your code, you should use it like this:

if ... then
  // code
elseif ... then
  // code
elseif ... then
  // code
else
  // code
end

So make those if's elseif's (except for the first one) and remove all end's except for the last one :P/>/>
Pedroc1999 #3
Posted 30 October 2012 - 10:25 PM
so i would make

if input open…
code..
elseif input close…
code…
elseif input os…
code…
else
print("incorrect code")
end
Pedroc1999 #4
Posted 30 October 2012 - 10:31 PM
i tried that but when i write something random no 'incorrect code message comes up'
Orwell #5
Posted 30 October 2012 - 10:34 PM
except that's still:

if input == "open" then
elseif input=="os" then
else
print('error')
end

I'm not sure if that's what you ment.
Also, in the code in your fi8rt post, you should add an extra end to close the while loop.

EDIT: add your current code please :P/>/>
Pedroc1999 #6
Posted 30 October 2012 - 10:35 PM
here is my code right now


os.pullEvent = os.pullEventRaw -- Disable CTRL-T
while true do -- Code Written By  Pedroc1999
term.clear()
term.setCursorPos(1, 1)
print("Please Enter Codes")
input = read("*")
if input == "open" then
  redstone.setOutput("back", false)
elseif input == "close" then
  redstone.setOutput("back", true)
elseif input == "os" then
  term.clear()
  term.setCursorPos(1, 1)
  print("Returning to CraftOS 1.4")
  sleep(1)
  print(" ")
  print("  3")
  sleep(1)
  print("  2")
  sleep(1)
  print("  1")
  sleep(1)
  term.clear()
  term.setCursorPos(1, 1)
  error() -- Simply Terminates Program
else
  print("Code Not Found")
 end
end
Orwell #7
Posted 30 October 2012 - 10:39 PM
Because there's 'term.clear()' in your loop, it clears everything before you can see it. You could add '
sleep(3)' after the 'print("error")'.
Pedroc1999 #8
Posted 30 October 2012 - 10:50 PM
i luv u man, not in guy way, it worked first time after adding that in, thx so much