3 posts
Posted 09 September 2012 - 05:43 PM
Hello,
Im working on a simple program for my nuclear reactor and since i don´t know how to make a good software, i have 1 computer for each reactor. But when i was finished with my code i got this error:
startup:2: attempted to index ? (a nil value)
Here is my code
term.clear()
Term.setCursorPos (1, 1)
Print ("Command prompt for Reactor 1")
if input == "start" then
redstone.setoutput ("back", false )
Print ("Command accepted, starting reactor 1")
if input == "stop" then
rs.setoutput ("back", true) else
print ("Invalid command")
os.reboot()
end
end
I am a newbie and i know it. But help will be much appreciated. Thanks
12 posts
Posted 09 September 2012 - 05:53 PM
Term.setCursorPos (1, 1)should be term.setCursorPos(1,1)caps matter.also you're calling for it to read the input variable bust not taking input anywhere,
local function start()
term.clear()
term.setCursorPos (1, 1)
Print ("Command prompt for Reactor 1")
input = read()
if input == "start" then
redstone.setOutput ("back", false )
Print ("Command accepted, starting reactor 1")
elseif input == "stop" then
redstone.setOutput ("back", true) else
print ("Invalid command")
os.reboot()
else
print("invalid input")
sleep(2)
start()
end
end
start()
by making it a function you can easily avoid errors from invalid input by restarting the function on invalid input as I did above.
Also, I formatted it for you :D/>/>
3 posts
Posted 09 September 2012 - 06:58 PM
Ohh, Big thanks dude! You are the best.
3 posts
Posted 09 September 2012 - 07:01 PM
Term.setCursorPos (1, 1)should be term.setCursorPos(1,1)caps matter.also you're calling for it to read the input variable bust not taking input anywhere,
local function start()
term.clear()
term.setCursorPos (1, 1)
Print ("Command prompt for Reactor 1")
input = read()
if input == "start" then
redstone.setOutput ("back", false )
Print ("Command accepted, starting reactor 1")
elseif input == "stop" then
redstone.setOutput ("back", true) else
print ("Invalid command")
os.reboot()
else
print("invalid input")
sleep(2)
start()
end
end
start()
by making it a function you can easily avoid errors from invalid input by restarting the function on invalid input as I did above.
Also, I formatted it for you :D/>/>
Sorry for bothering you but your code doesnt work either :/
Error:
bios:206: [string "startup"] :13: end expected (to close if at line 6)
8543 posts
Posted 09 September 2012 - 07:08 PM
Try this:
while true do
term.clear()
term.setCursorPos (1, 1)
print("Command prompt for Reactor 1")
input = read()
if input == "start" then
redstone.setOutput ("back", false )
print("Starting Reactor 1")
sleep(1)
elseif input == "stop" then
redstone.setOutput ("back", true)
print("Stopping Reactor 1")
sleep(1)
elseif input == "exit" then
break
else
print("Invalid command")
sleep(1)
end
end
The code Itstingray wrote was bad because it was calling a function from within itself to create a loop, which is bad. He also missed an else, which caused the error when the program encountered another else. Commands are now start, stop and exit.
14 posts
Posted 09 September 2012 - 07:36 PM
Try this:
while true do
term.clear()
term.setCursorPos (1, 1)
print("Command prompt for Reactor 1")
input = read()
if input == "start" then
redstone.setOutput ("back", false )
print("Starting Reactor 1")
sleep(1)
elseif input == "stop" then
redstone.setOutput ("back", true)
print("Stopping Reactor 1")
sleep(1)
elseif input == "exit" then
break
else
print("Invalid command")
sleep(1)
end
end
The code Itstingray wrote was bad because it was calling a function from within itself to create a loop, which is bad. He also missed an else, which caused the error when the program encountered another else. Commands are now start, stop and exit.
There is no problem in using recursive functions if you make sure that it does tail calls.
if you change the recursive call in itstingray program from start() to return start() it wil not overflow
the stack
8543 posts
Posted 09 September 2012 - 07:41 PM
Try this:
while true do
term.clear()
term.setCursorPos (1, 1)
print("Command prompt for Reactor 1")
input = read()
if input == "start" then
redstone.setOutput ("back", false )
print("Starting Reactor 1")
sleep(1)
elseif input == "stop" then
redstone.setOutput ("back", true)
print("Stopping Reactor 1")
sleep(1)
elseif input == "exit" then
break
else
print("Invalid command")
sleep(1)
end
end
The code Itstingray wrote was bad because it was calling a function from within itself to create a loop, which is bad. He also missed an else, which caused the error when the program encountered another else. Commands are now start, stop and exit.
There is no problem in using recursive functions if you make sure that it does tail calls.
if you change the recursive call in itstingray program from start() to return start() it wil not overflow
the stack
Technically true (in Lua), but still terrible practice when trying to create a loop. Recursion has its place, but it is no replacement for a good while loop. It's bad programming practice to use them where they don't belong, regardless of whether it would "work" or not.