Jump to content




What is Wrong with this program?



8 replies to this topic

#1 snaps1011

  • New Members
  • 1 posts

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

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 01 February 2014 - 01:00 AM

Moved to Ask a Pro.

#3 Farrk

  • Members
  • 23 posts

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 by Farrk, 01 February 2014 - 01:47 AM.


#4 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 01 February 2014 - 04:52 AM

Here is your code formatted with 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 by surferpup, 01 February 2014 - 04:55 AM.


#5 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

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


#6 Jim

  • Members
  • 33 posts
  • Locationtekkit.craftersland.net:25567

Posted 01 February 2014 - 04:22 PM

Shouldn't that be like

if guess == password1
?

Edited by Jim, 01 February 2014 - 04:23 PM.


#7 OReezy

  • Members
  • 91 posts

Posted 01 February 2014 - 04:41 PM

 Jim, on 01 February 2014 - 04:22 PM, said:

Shouldn't that be like

if guess == password1
?

No. password is the name of his variable.

#8 Jim

  • Members
  • 33 posts
  • Locationtekkit.craftersland.net:25567

Posted 02 February 2014 - 02:07 AM

Oh ye. Well, this is what you get for looking things up quickly..

#9 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 02 February 2014 - 05:03 PM

 Dog, on 01 February 2014 - 01:26 PM, said:

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 by surferpup, 02 February 2014 - 05:05 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users