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

Lights program (Error) Need help

Started by lewisholcombe, 23 February 2014 - 12:13 PM
lewisholcombe #1
Posted 23 February 2014 - 01:13 PM
Hey guys.

So I tried to make a program and its giving me back two errors.

Here's the error

Bios:206: [string "lights"]:50: 'end' expected
(to close 'while' at line 5)

and here is the code:


term.clear()
term.setCursorPos(16, 1)
print "Light Control System 0.01"
while true do
  term.setCursorPos(1, 3)
  term.clearLine()
  print "Options: Upstairs, Downstairs"
  write "Location: "
  local input = string.lower( read() )
  if input == "upstairs" then
  term.clear()
	    print "Upstairs Lights"
  write "On/Off: "
  local input = string.lower( read() )
 
  if input == "on" then
  term.clear()
  redstone.setOutput("left", true)
  print"Upstairs lights are on"
  end
  if input == "off" then
	    rs.setOutput("left", false)
	    print "Lights Off"
  end
 
    if input == "downstairs" then
  term.clear()
	    print "Downstairs Lights"
  write "On/Off: "
  local input = string.lower( read() )
 
    if input == "on" then
  term.clear()
  redstone.setOutput("right", true)
  print"Downstairs lights are on"
  end
  if input == "off" then
	    rs.setOutput("left", false)
	    print "Downstairs lights are off"
  end
end
  if input == "exit" then
	    break  -- Breaks out of the loop and thus exits the program (since there is nothing else outside of the loop in this case).
  end
end


Thanks in advance
MKlegoman357 #2
Posted 23 February 2014 - 01:43 PM
You are missing an end here:


...
	    print "Lights Off"
  end
     <<--<< Right here            <<--<< in there
    if input == "downstairs" then
  term.clear()
...

Indent your code, it helps to see missing ends and everything:

Here is your code, but indented (not fixed)

term.clear()
term.setCursorPos(16, 1)
print "Light Control System 0.01"
while true do
  term.setCursorPos(1, 3)
  term.clearLine()
  print "Options: Upstairs, Downstairs"
  write "Location: "
  local input = string.lower( read() )
  if input == "upstairs" then
    term.clear()
    print "Upstairs Lights"
    write "On/Off: "
    local input = string.lower( read() )

    if input == "on" then
      term.clear()
      redstone.setOutput("left", true)
      print"Upstairs lights are on"
    end
    if input == "off" then
      rs.setOutput("left", false)
      print "Lights Off"
    end

    if input == "downstairs" then <<-- And now we can see that this line doesn't match with 'if input == "upstairs" then'
      term.clear()
      print "Downstairs Lights"
      write "On/Off: "
      local input = string.lower( read() )

      if input == "on" then
        term.clear()
        redstone.setOutput("right", true)
        print"Downstairs lights are on"
      end
      if input == "off" then
        rs.setOutput("left", false)
        print "Downstairs lights are off"
      end
    end
    if input == "exit" then
      break  -- Breaks out of the loop and thus exits the program (since there is nothing else outside of the loop in this case).
    end
  end <<-- You can clearly see that there is a missing end

Also, you should use if … then … elseif … then … end instead of if … then … end if … then … end


if input == "upstairs" then
  ...
elseif input == "downstairs" then
  ..
end

This makes the code more readable.
Edited on 23 February 2014 - 12:45 PM