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

Need help understanding conditions

Started by ebarton97, 03 March 2014 - 03:05 AM
ebarton97 #1
Posted 03 March 2014 - 04:05 AM
Hey guys im pretty new to computercraft so this is going to likely be a simple question. I've been learning by reading other peoples codes and trying to understand why and what things do. I used someone elses code to make a door lock and it was very simple and worked just fine. I can edit it when you hold ctr-T and what i want to do is make a second password that allows acces to the terminal instead of turning off the computer.Here is the code i have in the computer
–Startup–

textutils.slowPrint("Enter Password")
password = read("*")
if password == "password" then
print("Password Accepted")
redstone.setOutput9"back", true)
sleep(5)
redstone.setOutput("back", false)
sleep(2)
os.shutdown()
end —this is where the original code ended
elseif password == "unlock" then —-this is the part i tried to add
print("Terminal Access Granted")
sleep(1)
end
end



if someone can explain whats wrong as well as why its wrong that would be great. I cant wait to learn more about the coding. Any other suggestions and explanations are welcome.

Also i had to type the code in here manually. Is there a way to copy and paste the code from tekkit so i dont have to retype it?
Alice #2
Posted 03 March 2014 - 04:34 AM
Original Post - Edited–I use [.namedspoiler="name"] and [.spoiler] to hide text.
Hey guys im pretty new to computercraft so this is going to likely be a simple question. I've been learning by reading other peoples codes and trying to understand why and what things do. I used someone elses code to make a door lock and it was very simple and worked just fine. I can edit it when you hold ctr-T and what i want to do is make a second password that allows acces to the terminal instead of turning off the computer.Here is the code i have in the computer
–Startup–

 --I use [code] and [./code] (without the .) to signal a code area.
textutils.slowPrint("Enter Password")
password = read("*")
if password == "password" then
print("Password Accepted")
redstone.setOutput9"back", true)
sleep(5)
redstone.setOutput("back", false)
sleep(2)
os.shutdown()
end						 ---REMOVE THIS - elseif means you don't need that end
elseif password == "unlock" then		 ----this is the part i tried to add
print("Terminal Access Granted")
sleep(1)
end--Also REMOVE THIS
end



if someone can explain whats wrong as well as why its wrong that would be great. I cant wait to learn more about the coding. Any other suggestions and explanations are welcome.

Also i had to type the code in here manually. Is there a way to copy and paste the code from tekkit so i dont have to retype it?
Edited your post^ a small bit - also, read up on how to use the 'pastebin' program.
Edited on 03 March 2014 - 03:36 AM
unobtanium #3
Posted 03 March 2014 - 11:08 AM
You just messed up the positioning of your added elseif-statement. You wrote it outside the if-statement and is not detected to interact with anything, which is why it probably throws an error.


textutils.slowPrint("Enter Password")
password = read("*")
if password == "password" then
print("Password Accepted")
redstone.setOutput9"back", true)
sleep(5)
redstone.setOutput("back", false)
sleep(2)
os.shutdown()
-- HERE WAS AN END WHICH WAS NOT NEEDED
elseif password == "unlock" then
print("Terminal Access Granted")
sleep(1)
end

By deleting this "end" the program will check (password == "unlock") if the statement(s) before (password == "password") were all false.
Edited on 04 March 2014 - 11:49 PM
Lyqyd #4
Posted 03 March 2014 - 03:29 PM
You missed the end on the last line. The two that need to be removed are the end before the elseif and the last end of the script.
MKlegoman357 #5
Posted 03 March 2014 - 04:08 PM
You don't need an end after every if/elseif:


if (condition) then
  ...
elseif (condition) then
  ...
else
  ...
end