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

Need help with a door lock

Started by Kron, 09 March 2013 - 03:44 PM
Kron #1
Posted 09 March 2013 - 04:44 PM
Hello guys. I'm currently creating a computer called 'Artificial Intelligence'. I label it is AI, however it only has two commands, and I'm naming it AI since it actually 'talks' to the user. However, whenever I type, I keep getting:
No such program

Here is my code.

term.clear()
term.setCursorPos(1,1)
print("Artificial Intelligence OS 1.4")
print("Hello. I am the Artificial Intelligence of Cron[I]. Welcome, and please enjoy your stay. Would you like to enter?")
print(" ")
print(" [1] = Yes")
print(" [2] = No")
write(">> ")
if input == "1" then
print("Have a nice day!")
redstone.setOutput("left", true)
sleep(4)
redstone.setOutput("left", false)
os.reboot()
if input  == "2" then
print("Okay. Sorry for bothering ya'!")
sleep(2)
os.reboot()
else
print("I don't understand you. My circuts only respond to  ones and twos.")
sleep(3)
os.reboot()
end
end


How do I fix this?
SuicidalSTDz #2
Posted 09 March 2013 - 04:48 PM
You are not defining the variable input.

input = read()

Did you save the program? If so, it should not be returning "No Such Program"
LuaEclipser #3
Posted 09 March 2013 - 05:05 PM
you forgot the "else". it is continueing the program. look at this VERY careful and see what i mean


term.clear()
term.setCursorPos(1,1)
print("Artificial Intelligence OS 1.4")
print("Hello. I am the Artificial Intelligence of Cron[I]. Welcome, and please enjoy your stay. Would you like to enter?")
print(" ")
print(" [1] = Yes")
print(" [2] = No")
write(">> ")
if input == "1" then
print("Have a nice day!")
redstone.setOutput("left", true)
sleep(4)
redstone.setOutput("left", false)
os.reboot()
elseif input  == "2" then     ------- the elseif is doing else and if at the same time :3
print("Okay. Sorry for bothering ya'!")
sleep(2)
os.reboot()
else
print("I don't understand you. My circuts only respond to  ones and twos.")
sleep(3)
os.reboot()
end
end


should work now
Kron #4
Posted 09 March 2013 - 05:06 PM
You are not defining the variable input.

input = read()

Did you save the program? If so, it should not be returning "No Such Program"

I did save, and where would I put input = read()?

you forgot the "else". it is continueing the program. look at this VERY careful and see what i mean


term.clear()
term.setCursorPos(1,1)
print("Artificial Intelligence OS 1.4")
print("Hello. I am the Artificial Intelligence of Cron[I]. Welcome, and please enjoy your stay. Would you like to enter?")
print(" ")
print(" [1] = Yes")
print(" [2] = No")
write(">> ")
if input == "1" then
print("Have a nice day!")
redstone.setOutput("left", true)
sleep(4)
redstone.setOutput("left", false)
os.reboot()
elseif input  == "2" then	 ------- the elseif is doing else and if at the same time :3
print("Okay. Sorry for bothering ya'!")
sleep(2)
os.reboot()
else
print("I don't understand you. My circuts only respond to  ones and twos.")
sleep(3)
os.reboot()
end
end


should work now

Okay, I'll try this.

Edit: With this modified code, I get: bios:206: [string "startup"]:22: '<eof>' expected
LuaEclipser #5
Posted 09 March 2013 - 05:09 PM
do this thing lol i forgot somthing


term.clear()
term.setCursorPos(1,1)
print("Artificial Intelligence OS 1.4")
print("Hello. I am the Artificial Intelligence of Cron. Welcome, and please enjoy your stay. Would you like to enter?")
print(" ")
print(" [1] = Yes")
print(" [2] = No")
write(">> ")
if input == "1" then
print("Have a nice day!")
redstone.setOutput("left", true)
sleep(4)
redstone.setOutput("left", false)
os.reboot()
if input == "2" then
print("Okay. Sorry for bothering ya'!")
sleep(2)
os.reboot()
else
print("I don't understand you. My circuts only respond to ones and twos.")
sleep(3)
os.reboot()
end
end
SuicidalSTDz #6
Posted 09 March 2013 - 05:09 PM
Input = read() should be placed before you start the if statement block. ^^ Code format please. Also, i would use a repeat until loop.
ChunLing #7
Posted 09 March 2013 - 05:39 PM
Yeah, or just return out of the program rather than rebooting.

The above code had an extra end, by the way. That's generally (though not always) what EOF expected means.
Edited on 09 March 2013 - 04:40 PM
SuicidalSTDz #8
Posted 09 March 2013 - 05:44 PM
Yeah, or just return out of the program rather than rebooting.

The above code had an extra end, by the way. That's generally (though not always) what EOF expected means.
Anything is better than rebooting… Well except for shutting down ;)/>
ChunLing #9
Posted 09 March 2013 - 05:53 PM
Well…there's always the old recursing the function to loop it trick. I do not consider that better than rebooting. But yeah, rebooting is very resource intensive because it forces the entire system to reload, all the apis and the shell need to reload on that instance of the computer. It's sometimes worth doing, but not often.
SuicidalSTDz #10
Posted 09 March 2013 - 05:58 PM
A better recursion method would be something like this: (I might be wrong)

local tableThing = {"hehe","lol","hmm")
local function = someFunction()
 term.clear()
 for k,v in pairs(tableThing) do
  print(v)
 end
 return someFunction() --Not sure if returning a function would be better
end
ChunLing #11
Posted 09 March 2013 - 06:15 PM
…no. Just no. This will inevitably (and very quickly) result in overflowing the stack, because no matter what, the function has to call itself to get a return. It will print out the contents of the table a bunch of times before the stack overflows.

I did not mean to suggest that recursing was a good idea, I was pointing out that it would be a worse idea that rebooting.
SuicidalSTDz #12
Posted 09 March 2013 - 06:32 PM
…no. Just no. This will inevitably (and very quickly) result in overflowing the stack, because no matter what, the function has to call itself to get a return. It will print out the contents of the table a bunch of times before the stack overflows.

I did not mean to suggest that recursing was a good idea, I was pointing out that it would be a worse idea that rebooting.
I see now, thanks for clearing that up. I was not suggesting recursion was better either ;)/>