2 posts
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
209 posts
Location
In your fridge.
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."
2088 posts
Location
South Africa
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
209 posts
Location
In your fridge.
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.
2088 posts
Location
South Africa
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.
2 posts
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!
2088 posts
Location
South Africa
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 :)/>/>
252 posts
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.
2088 posts
Location
South Africa
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 :)/>/>
252 posts
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
8543 posts
Posted 14 October 2012 - 11:17 AM
It's string.lower(), actually.
2088 posts
Location
South Africa
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 :)/>/>