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

What is Wrong with this program?

Started by snaps1011, 31 January 2014 - 09:29 PM
snaps1011 #1
Posted 31 January 2014 - 10:29 PM
shell.run("startup")

door = 'left'
alarm = 'bottom'
password = 'turtle'
password2 = 'FOREVER'
mon = peripheral.wrap("right")

term.clear()
term.setCursorPos(1,1)
print('Password Please')
guess = read('*')
if password == guess then
mon.clear()
mon.setTextScale(1)
mon.setCursorPos
mon.write("Proceed")
rs.setOutput(door,true)
sleep(15)
rs.setOutput(door,false)

if password2 == guess then
mon.clear()
mon.setTextScale(1)
mon.setCursorPos(5,5)
mon.write("Door Open Forever")
rs.setOutput(door,true)

else
rs.setOutput(alarm,true)
mon.clear()
mon.setTextScale(1)
mon.setCursorPos(5,5)
mon.write("Remove your self")
sleep(20)
rs.setOutput(alarm,false)
mon.clear()
end
end



whats wrong here? evertime I run this and put in it comes up with Proceed on the monitors and then after a bit goes to remove yourself and sounds the alarm
Lyqyd #2
Posted 01 February 2014 - 01:00 AM
Moved to Ask a Pro.
Farrk #3
Posted 01 February 2014 - 01:46 AM
Looks kind of derpy - you only check if guess was password2 if guess is password which doesnt make any sense. Remove end in last line and change

if password2 == guess then
to

elseif password2 == guess then
Edited on 01 February 2014 - 12:47 AM
surferpup #4
Posted 01 February 2014 - 04:52 AM
Here is your code formatted with [member='Farrk']'s correction. It doesn't look too derpy, just made a mistake on the if .. elseif .. else .. end construct.


shell.run("startup")
door = 'left'
alarm = 'bottom'
password = 'turtle'
password2 = 'FOREVER'
mon = peripheral.wrap("right")
term.clear()
term.setCursorPos(1,1)
print('Password Please')
guess = read('*')
if password == guess then
	mon.clear()
	mon.setTextScale(1)
	mon.setCursorPos
	mon.write("Proceed")
	rs.setOutput(door,true)
	sleep(15)
	rs.setOutput(door,false)
elseif password2 == guess then
	mon.clear()
	mon.setTextScale(1)
	mon.setCursorPos(5,5)
	mon.write("Door Open Forever")
	rs.setOutput(door,true)
else
	rs.setOutput(alarm,true)
	mon.clear()
	mon.setTextScale(1)
	mon.setCursorPos(5,5)
	mon.write("Remove your self")
	sleep(20)
	rs.setOutput(alarm,false)
	mon.clear()
end

Are you sure you want to do the shell.run("startup") at the beginning there?

Everybody starts coding in Lua with something. It is the only way to learn :)/>
Edited on 01 February 2014 - 03:55 AM
Dog #5
Posted 01 February 2014 - 01:26 PM
Here's the cleaned up code with all the variables localized…


shell.run("startup")
local door = 'left'
local alarm = 'bottom'
local password = 'turtle'
local password2 = 'FOREVER'
local mon = peripheral.wrap("right")
term.clear()
term.setCursorPos(1,1)
print('Password Please')
local guess = read('*')
if password == guess then
		mon.clear()
		mon.setTextScale(1)
		mon.setCursorPos
		mon.write("Proceed")
		rs.setOutput(door,true)
		sleep(15)
		rs.setOutput(door,false)
elseif password2 == guess then
		mon.clear()
		mon.setTextScale(1)
		mon.setCursorPos(5,5)
		mon.write("Door Open Forever")
		rs.setOutput(door,true)
else
		rs.setOutput(alarm,true)
		mon.clear()
		mon.setTextScale(1)
		mon.setCursorPos(5,5)
		mon.write("Remove your self")
		sleep(20)
		rs.setOutput(alarm,false)
		mon.clear()
end
Jim #6
Posted 01 February 2014 - 04:22 PM
Shouldn't that be like


if guess == password1
?
Edited on 01 February 2014 - 03:23 PM
OReezy #7
Posted 01 February 2014 - 04:41 PM
Shouldn't that be like


if guess == password1
?

No. password is the name of his variable.
Jim #8
Posted 02 February 2014 - 02:07 AM
Oh ye. Well, this is what you get for looking things up quickly..
surferpup #9
Posted 02 February 2014 - 05:03 PM
Here's the cleaned up code with all the variables localized…

Now here's the cleaned up code with all the errors removed:


--shell.run("startup")
local door = 'left'
local alarm = 'bottom'
local password = 'turtle'
local password2 = 'FOREVER'
local mon = peripheral.wrap("right")
term.clear()
term.setCursorPos(1,1)
print('Password Please')
local guess = read('*')
if password == guess then
	mon.clear()
	mon.setTextScale(1)
	mon.setCursorPos(5,5)
	mon.write("Proceed")
	rs.setOutput(door,true)
	sleep(15)
	rs.setOutput(door,false)
elseif password2 == guess then
	mon.clear()
	mon.setTextScale(1)
	mon.setCursorPos(5,5)
	mon.write("Door Open Forever")
	rs.setOutput(door,true)
else
	rs.setOutput(alarm,true)
	mon.clear()
	mon.setTextScale(1)
	mon.setCursorPos(5,5)
	mon.write("Remove your self")
	sleep(20)
	rs.setOutput(alarm,false)
	mon.clear()
end

In the if password == guess block, setCursorPos did not have a cursor position argument specified. I supplied one.

It seems to work fine now. I commented out the first line, because it is of no use in my testing.
Edited on 02 February 2014 - 04:05 PM