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

Help my program out?

Started by BlockDriller, 17 July 2013 - 03:48 PM
BlockDriller #1
Posted 17 July 2013 - 05:48 PM
It doesn't seem to be working, anyone have any ideas?


print("Activate/Deactivate?")
print("Y/N?")
Yes = input
if Yes == "Y" then
print("Activating Bin...")
sleep(3)
print("Activated.")
redstone.setOutput("right", true)
end
else
if Yes == "N" then
print("Deactivating Bin..."
sleep(3)
print("Deactivated.")
redstone.setOutput("right", false)
end
else
print("Command not recognized.")
sleep(2)
shell.run("Bin")
end
ZagKalidor #2
Posted 17 July 2013 - 06:24 PM

print("Activate/Deactivate?")
print("Y/N?")
Yes = io.read()
if Yes == "Y" then print("Activating Bin...")
sleep(3)
print("Activated.")
rs.setOutput("right", true)

elseif Yes == "N" then print("Deactivating Bin...")
sleep(3)
print("Deactivated.")
rs.setOutput("right", false)
else
print("Command not recognized.")
sleep(2)
shell.run("Bin")
end

You put the value of variable input to the variable Yes instead of calling a user input.
io.read() does that.

Some ends where set in a wrong position. there is no end between if, elseif and else. Its if, elseif, else and at least end

Also note that the userinput has to be a capital letter (pushed with the shift key). Otherwise it will set Command not recognized…

The thing i like most in this little progie is if Yes = Y and If Yes = N …. very nice, it inspires me..
That can only be understood by a dump computer. A philosoph would answer - "What if No was Yes and is there an end of if?"

haha
greetz
Edited on 17 July 2013 - 04:45 PM
albrat #3
Posted 17 July 2013 - 07:51 PM
Hi, I can see a few problems in your program…


bin = true  -- new line to create loop varible
while bin do -- start loop.
print("Activate/Deactivate?")
print("Y/N?")
input = read():lower()  -- Get y or n in lower case
if input == "y" then   -- check only lower case
print("Activating Bin...")
sleep(3)
print("Activated.")
redstone.setOutput("right", true)
bin = false -- exit loop
														  -- removed end
elseif input == "n" then   -- elseif  to check a second value (lower case only)
print("Deactivating Bin..."
sleep(3)
print("Deactivated.")
redstone.setOutput("right", false)
bin = false -- exit loop
														    -- removed end
else													 -- if not y or n then...
print("Command not recognized.")
sleep(2)
-- shell.run("Bin")  -- Nooo please don't recall the program like this.
end 
end -- added end to close out loop

This code above should work correctly, (calling a program from within itself is a bad idea - there is a chace that it can overload the computer and crash).
True of any program. Imagine running minecraft, that starts minecraft, that starts minecraft. (if your game assigns 1gb ram each time… it only takes a few times of calling itself and we're out of memory)…
resaloli #4
Posted 18 July 2013 - 01:24 AM
Hi, I can see a few problems in your program…


bin = true  -- new line to create loop varible
while bin do -- start loop.
print("Activate/Deactivate?")
print("Y/N?")
input = read():lower()  -- Get y or n in lower case
if input == "y" then   -- check only lower case
print("Activating Bin...")
sleep(3)
print("Activated.")
redstone.setOutput("right", true)
bin = false -- exit loop
														  -- removed end
elseif input == "n" then   -- elseif  to check a second value (lower case only)
print("Deactivating Bin..."
sleep(3)
print("Deactivated.")
redstone.setOutput("right", false)
bin = false -- exit loop
															-- removed end
else													 -- if not y or n then...
print("Command not recognized.")
sleep(2)
-- shell.run("Bin")  -- Nooo please don't recall the program like this.
end
end -- added end to close out loop

This code above should work correctly, (calling a program from within itself is a bad idea - there is a chace that it can overload the computer and crash).
True of any program. Imagine running minecraft, that starts minecraft, that starts minecraft. (if your game assigns 1gb ram each time… it only takes a few times of calling itself and we're out of memory)…

that will not overload the computer and dont compare it to real computers that you cant acidently start 2 instaces of a game, when he uses shell.run("bin") that will just restart the program so heres the original code with some fixs:

function reset()
	term.clear()
	term.setCursorPos(1,1)
	shell.run("shell")
end
while true do
	print("Activate/Deactivate?")
	print("Y/N?")
	local input = read()
	if input == "Y" then
		print("Activating Bin...")
		sleep(3)
		print("Activated.")
		redstone.setOutput("right", true)
		sleep(1)
		reset()
	elseif input == "N" then
			print("Deactivating Bin...")
			sleep(3)
			print("Deactivated.")
			redstone.setOutput("right", false)
			sleep(1)
			reset()
	else
		print("Command not recognized.")
		sleep(2)
	end
end

So all the changes i did was
  • organized the code(use TAB to create spaces to know what is inside what)
  • remove execesses end between then if-elseif-end like ZagKalidor sayid before
  • changed the variable "Yes = input" to "local input = read()"
  • instead of starting again the program all time it inputs a wrong command it will just start reading all over like like albrat sayid before but cleaner
  • added a function to terminate the program when it receives the right input(Y/N) if you want to it run all time just remove the function calls at lines 16 and 23
so there is your code ready to use


BTW i used shell.run("shell") to stop the program instead of shell.exit() because i couldn't get it stoping so i just started the shell over
BlockDriller #5
Posted 18 July 2013 - 06:18 AM
Hi, I can see a few problems in your program…


bin = true  -- new line to create loop varible
while bin do -- start loop.
print("Activate/Deactivate?")
print("Y/N?")
input = read():lower()  -- Get y or n in lower case
if input == "y" then   -- check only lower case
print("Activating Bin...")
sleep(3)
print("Activated.")
redstone.setOutput("right", true)
bin = false -- exit loop
														  -- removed end
elseif input == "n" then   -- elseif  to check a second value (lower case only)
print("Deactivating Bin..."
sleep(3)
print("Deactivated.")
redstone.setOutput("right", false)
bin = false -- exit loop
															-- removed end
else													 -- if not y or n then...
print("Command not recognized.")
sleep(2)
-- shell.run("Bin")  -- Nooo please don't recall the program like this.
end
end -- added end to close out loop

This code above should work correctly, (calling a program from within itself is a bad idea - there is a chace that it can overload the computer and crash).
True of any program. Imagine running minecraft, that starts minecraft, that starts minecraft. (if your game assigns 1gb ram each time… it only takes a few times of calling itself and we're out of memory)…

that will not overload the computer and dont compare it to real computers that you cant acidently start 2 instaces of a game, when he uses shell.run("bin") that will just restart the program so heres the original code with some fixs:

function reset()
	term.clear()
	term.setCursorPos(1,1)
	shell.run("shell")
end
while true do
	print("Activate/Deactivate?")
	print("Y/N?")
	local input = read()
	if input == "Y" then
		print("Activating Bin...")
		sleep(3)
		print("Activated.")
		redstone.setOutput("right", true)
		sleep(1)
		reset()
	elseif input == "N" then
			print("Deactivating Bin...")
			sleep(3)
			print("Deactivated.")
			redstone.setOutput("right", false)
			sleep(1)
			reset()
	else
		print("Command not recognized.")
		sleep(2)
	end
end

So all the changes i did was
  • organized the code(use TAB to create spaces to know what is inside what)
  • remove execesses end between then if-elseif-end like ZagKalidor sayid before
  • changed the variable "Yes = input" to "local input = read()"
  • instead of starting again the program all time it inputs a wrong command it will just start reading all over like like albrat sayid before but cleaner
  • added a function to terminate the program when it receives the right input(Y/N) if you want to it run all time just remove the function calls at lines 16 and 23
so there is your code ready to use


BTW i used shell.run("shell") to stop the program instead of shell.exit() because i couldn't get it stoping so i just started the shell over

Yours worked fine, thanks!
albrat #6
Posted 18 July 2013 - 06:47 AM
the reset() function could also be replaced with os.reboot() which basically does the same.
resaloli #7
Posted 18 July 2013 - 10:10 AM
the reset() function could also be replaced with os.reboot() which basically does the same.

why reboot when you can instally get to the shell? and if the program was named as startup?
GravityScore #8
Posted 18 July 2013 - 12:01 PM
why reboot when you can instally get to the shell? and if the program was named as startup?

Because calling shell.run("shell") will cause a stack overflow eventually. That, and it's really bad practice. Use error() instead of shell.run("shell") (just replace the shell.run line with error()).
BlockDriller #9
Posted 18 July 2013 - 03:36 PM
the reset() function could also be replaced with os.reboot() which basically does the same.

Yeah but if the redstone is on then if you accidentally put 'J' for example, it turns it off.
ZagKalidor #10
Posted 19 July 2013 - 06:13 AM
I think, putting it into a function and then recall the function inside a loop would be nicer than recalling the whole program