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

Code help

Started by lazerpickle, 13 October 2012 - 11:32 PM
lazerpickle #1
Posted 14 October 2012 - 01:32 AM
Can anyone help me with my code? I'm basically trying to make a program that asks for an input of either "on" or "off", then changes the redstone output accordingly and notifies the user. But i can't seem to get it right, any obvious errors here?

[indent=1]

x = 1[/indent]
while x = 1 do

print("Would you like to turn the cobblestone generator on or off? (use inputs ON or OFF)"

input = io.read()

if input == "On" then

  redstone.setOutput("back", true)

elseif input == "Off" then

  redstone.setOutput("back", false)

else

  print("Command not recognized, restarting...")

  sleep(2)

  x = 0

end

if redstone.getInput("back") == true then

  print("The cobblestone generator has been turned on")

elseif redstone.getInput("back") == false then

  print("The cobblestone generator has been turned off")

end

sleep(5)

term.clear()

x = 0
end
Lettuce #2
Posted 14 October 2012 - 01:43 AM
Are you getting an error message? Are you typing "ON" or "OFF?" Because your program only accepts "On" or "Off."
remiX #3
Posted 14 October 2012 - 01:45 AM
You have a lot of unnecessary stuff in your code. Here's a shorter and working (hopefully - didn't test it but im sure it would work) code.


while true do
	print("Would you like to turn the cobblestone generator on or off? (use inputs ON or OFF")
	input = read()
	if input == "On" or input == "on" or input == "ON" then
		redstone.setOutput("back", true)
	elseif input == "Off" or input == "off" or input == "OFF" then
		redstone.setOutput("back", false)
	else
		print("Command not recognized, restarting...")
		sleep(2)
	end
	print("The cobblestone generator has been turned "..input..".")
	sleep(5)
	term.clear()
	term.setCursorPos(1,1)
end
Lettuce #4
Posted 14 October 2012 - 01:48 AM
Usually sidekick, it's better to let them keep their own code, and make thir own improvements, then give tips so they can change it themselves'. They learn better that way. Just saying.
remiX #5
Posted 14 October 2012 - 02:04 AM
Usually sidekick, it's better to let them keep their own code, and make thir own improvements, then give tips so they can change it themselves'. They learn better that way. Just saying.

Just showing him another and easier way of doing it.
lazerpickle #6
Posted 14 October 2012 - 04:01 AM
Oh sorry I forgot to check back, I fixed the code already perhaps I was too hasty in posting here before debugging it myself thank you though!
remiX #7
Posted 14 October 2012 - 04:23 AM
Oh sorry I forgot to check back, I fixed the code already perhaps I was too hasty in posting here before debugging it myself thank you though!

Thats what they all say :)/>/>
ChaddJackson12 #8
Posted 14 October 2012 - 04:48 AM
You have a lot of unnecessary stuff in your code. Here's a shorter and working (hopefully - didn't test it but im sure it would work) code.


while true do
	print("Would you like to turn the cobblestone generator on or off? (use inputs ON or OFF")
	input = read()
	if input == "On" or input == "on" or input == "ON" then
		redstone.setOutput("back", true)
	elseif input == "Off" or input == "off" or input == "OFF" then
		redstone.setOutput("back", false)
	else
		print("Command not recognized, restarting...")
		sleep(2)
	end
	print("The cobblestone generator has been turned "..input..".")
	sleep(5)
	term.clear()
	term.setCursorPos(1,1)
end
This may help, but I saw a way to have the input be read all lowercase so you don't have to have 3 different input options like you do. The bad part is: I forgot what it was xD. I think it was like: input = read(input.lowercase()) or something. Sorry I can't remember.
remiX #9
Posted 14 October 2012 - 04:53 AM
This may help, but I saw a way to have the input be read all lowercase so you don't have to have 3 different input options like you do. The bad part is: I forgot what it was xD. I think it was like: input = read(input.lowercase()) or something. Sorry I can't remember.

Oh yeah, you mean string.lowercase(read())


while true do
        print("Would you like to turn the cobblestone generator on or off? (use inputs ON or OFF")
        input = string.lower(read())
        if input == "on" then
                redstone.setOutput("back", true)
        elseif input == "off" then
                redstone.setOutput("back", false)
        else
                print("Command not recognized, restarting...")
                sleep(2)
        end
        print("The cobblestone generator has been turned "..input..".")
        sleep(5)
        term.clear()
        term.setCursorPos(1,1)
end

I actually forgot about that :)/>/>
ChaddJackson12 #10
Posted 14 October 2012 - 05:06 AM
This may help, but I saw a way to have the input be read all lowercase so you don't have to have 3 different input options like you do. The bad part is: I forgot what it was xD. I think it was like: input = read(input.lowercase()) or something. Sorry I can't remember.

Oh yeah, you mean string.lowercase(read())


while true do
        print("Would you like to turn the cobblestone generator on or off? (use inputs ON or OFF")
        input = string.lower(read())
        if input == "on" then
                redstone.setOutput("back", true)
        elseif input == "off" then
                redstone.setOutput("back", false)
        else
                print("Command not recognized, restarting...")
                sleep(2)
        end
        print("The cobblestone generator has been turned "..input..".")
        sleep(5)
        term.clear()
        term.setCursorPos(1,1)
end

I actually forgot about that :)/>/>
Yeah. That, and I am gonna start using it too because I never have before :3
Lyqyd #11
Posted 14 October 2012 - 11:17 AM
It's string.lower(), actually.
remiX #12
Posted 14 October 2012 - 11:19 AM
It's string.lower(), actually.

Yeah, thats what I'm using?

Edit: Hmm, my bad. In my post I had it as string.lowercase() but in the code I had it right… Didn't even realise :)/>/>