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

While loops, loop limit

Started by estrong1, 29 March 2013 - 10:01 PM
estrong1 #1
Posted 29 March 2013 - 11:01 PM
Hello guys I've been trying to make a password protected door on a server that I play on.
I want it to loop 3 times (3 guesses) before it ends the loop and outputs a redstone signal.
I've been working on it for a bit and just can't seem to figure it out (I'm a noob so don't go too hard)


os.pullEvent = os.pullEventRaw
local password = "password123"
local guess = 0
  while do
    term.clear()
    term.setCursorPos(1,1)
    write("Password: ")
    local input = read("*")
    if input == password then
    term.clear()
    term.setCursorPos(1,1)
    print("Correct!")
    rs.setOutput("right",true)
    sleep(5)
    rs.setOutput("right",false)
  else
    print("Try again...")
    guess = guess + 1

if guess == 3 then
  print("Good bye!")
  rs.setOutput("back",true)
  sleep(5)
  rs.setOutput("back",false)
  end
end

Again I'm not a pro coder or anything, but if you could point out my errors and tell me how I might fix them it would be much appreciated!

Thanks again!
faubiguy #2
Posted 29 March 2013 - 11:09 PM
while loops use a true/false condition to determine whether they should keep running or not, in the form
while <condition> do
  --Loop body
end
If you want the code to loop forever, or until exited with the break keyword, you can just use true for the condition.
superaxander #3
Posted 29 March 2013 - 11:09 PM
What you do works but you need to do
while true do
Or you can do this
while not guess == 3
Mads #4
Posted 29 March 2013 - 11:11 PM
Or this:

for i = 1, 3 do
    -- Do stuff
end
superaxander #5
Posted 29 March 2013 - 11:17 PM
Yeh I know but just to keep it simple for him/her
estrong1 #6
Posted 30 March 2013 - 01:53 AM
Thanks for the help guys I'll try it out.
estrong1 #7
Posted 30 March 2013 - 02:04 AM
What you do works but you need to do
while true do
Or you can do this
while not guess == 3

So I tried what you mentioned here, I'm apparently not doing it right as I still am getting errors, does this look correct to you?


os.pullEvent = os.pullEventRaw
local password = "password123"
local guess = 0
  while not guess == 3 do
    term.clear()
    term.setCursorPos(1,1)
    write("Password: ")
    local input = read("*")
  if input == password then
    term.clear()
    term.setCursorPos(1,1)
    print("Correct!")
    rs.setOutput("right",true)
    sleep(5)
    rs.setOutput("right",false)
  else
    print("Try again...")
    guess = guess + 1

while guess == 3 do
  print("Good bye!")
  rs.setOutput("back",true)
  sleep(5)
  rs.setOutput("back",false)
  end
end
slango20 #8
Posted 30 March 2013 - 01:02 PM
What you do works but you need to do
while true do
Or you can do this
while not guess == 3

So I tried what you mentioned here, I'm apparently not doing it right as I still am getting errors, does this look correct to you?


os.pullEvent = os.pullEventRaw
local password = "password123"
local guess = 0
  while not guess == 3 do
	term.clear()
	term.setCursorPos(1,1)
	write("Password: ")
	local input = read("*")
  if input == password then
	term.clear()
	term.setCursorPos(1,1)
	print("Correct!")
	rs.setOutput("right",true)
	sleep(5)
	rs.setOutput("right",false)
  else
	print("Try again...")
	guess = guess + 1

while guess == 3 do
  print("Good bye!")
  rs.setOutput("back",true)
  sleep(5)
  rs.setOutput("back",false)
  end
end
try this:
while guess < 3 do
--code
guess = guess + 1
that is what I know, and what error are you getting?
superaxander #9
Posted 30 March 2013 - 07:24 PM
You miss an end of the if statement
TheOddByte #10
Posted 31 March 2013 - 12:21 PM
Uhmm.. Here's a code that should work..
SpoilerAdded a function named cleanScreen() that
is basically the term.clear() term.setCursorPos(1,1)
But you just have to type cleanScreen()



os.pullEvent = os.pullEventRaw
local password = "password123"
guess = 0

function cleanScreen()
term.clear()
  term.setCursorPos(1,1)
end

function main()

  for i = 1,3 do
    cleanScreen()
      write("Password: ")
         local input = read("*")
      if input == password then
    cleanScreen()
    print("Correct!")
    rs.setOutput("right",true)
    sleep(5)
    rs.setOutput("right",false)


  elseif input ~= password then
    print("Try again...")
    guess = guess + 1
sleep(1)
end
  end

if guess == 3 then
   cleanScreen()
  print("Good bye!")
  rs.setOutput("back",true)
  sleep(5)
  rs.setOutput("back",false)
  end
end


main()

Uhmm.. Here's a code that should work..
SpoilerAdded a function named cleanScreen() that
is basically the term.clear() term.setCursorPos(1,1)
But you just have to type cleanScreen()


os.pullEvent = os.pullEventRaw
local password = "password123"
guess = 0

function cleanScreen()
term.clear()
  term.setCursorPos(1,1)
end

function main()

  for i = 1,3 do
    cleanScreen()
      write("Password: ")
         local input = read("*")
      if input == password then
    cleanScreen()
    print("Correct!")
    rs.setOutput("right",true)
    sleep(5)
    rs.setOutput("right",false)

<p> %2