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

nuclear reactor control, errors.

Started by LevenIce, 28 September 2016 - 01:51 PM
LevenIce #1
Posted 28 September 2016 - 03:51 PM
I want to make a program that ask if i want to shut down or start up the reactor by sending a redstone signal.
I have made a startup program but some errors have occoured due to my lack of "skill"
The error code that i get: [String "startup"]:4: ´)´ expected

on = "on"
off = "off"
close = "close"
print ("type "on" to enable, "off" to shut down")
if input == on then
	 print ("Nuclear reactor enabled.")
	 rs.setOutput ("back", true)
	 elseif input == off then
	 print ("Nuclear reactor disabled")
	 rs.setOutput ("back", false)
		elseif input == close then
		print ("GoodBye")
		shell.run("close")
	   else
		print ("?")
		sleep(2)
		os.shutdown
end

I'm short on time so, any quick response is highly appriciated
Bomb Bloke #2
Posted 29 September 2016 - 12:39 AM
print ("type "on" to enable, "off" to shut down")

You've messed up your quotes. Escape characters provide one way around it:

print ("type \"on\" to enable, \"off\" to shut down")
Dog #3
Posted 29 September 2016 - 12:53 AM
You can also use single quotes and double quotes together like this…

print("type 'on' to enable, 'off' to shut down")

or this…

print('type "on" to enable, "off" to shut down')


Additionally, you can get rid of on == "on", off == "off", and close == "close" (which isn't even used) if you convert your comparisons to strings (you need to enclose on and off within quotes). I would also suggest using string.lower() on input so the user can type "ON" or "On" or "oN", etc. and it'll still work since input will be converted to lower case for the comparison. In your code you aren't capturing user input anywhere, so I've added a call to read() and string.lower() and I've localized your input variable so it's local to the program and is slightly faster…

print("Type 'on' to enable, 'off' to shut down")
local input = string.lower(read()) --# using string.lower to convert user's input to lower case
if input == "on" then --# enclosing on in quotes
  --# code here
elseif input == "off" then --# enclosing off in quotes
  --# code here
else
  --# code here
end