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

Missing End Statement Where One Is Not Needed

Started by DARKHAWX, 21 February 2014 - 10:16 PM
DARKHAWX #1
Posted 21 February 2014 - 11:16 PM
So I was making a little test program just for a bit of fun and to refresh my memory on LUA programming and ran into a slight problem while testing it. Every time I execute the program it returns the error:
bios:339: [string "maze"]:30: 'end' expected (to close 'while' at line 9) 
I understand that it is telling me to add an end to close the while loop I started at line 9. However, I have a lot more code that must be executed within the while loop and so if I close it that code will not be executed. So could one of you Pros check my code and find out what to do to fix this error.

--Variables
local completed = false
local modem = peripheral.wrap("right")
local x = 1
local y = 1
local dir = 270
--Movement
while completed == false do
  if turtle.getFuelLevel() < 16 then
	turtle.refuel(1)
  end

  if turtle.detect() then
	turtle.turnLeft()
	dir = dir + 90
	if turtle.detect() then
	  turtle.turnRight()
	  turtle.turnRight()
	  dir = dir - 180
	  if turtle.detect() then
		turtle.turnRight()
		dir = dir - 90
		turtle.forward()
		end
	  else turtle.forward()
	  end
	else turtle.forward()
	end
  else turtle.forward()
  end
  
  --Send Coordinates
  if dir >= 360 then
	dir = dir - 360
  end
  if dir < 0 then
	dir = dir + 360
  end

  if dir == 0 then
	x = x + 1
	end
  else if dir == 90 then
	y = y + 1
	end
  else if dir == 180 then
	x = x - 1
	end
  else if dir == 270 then
	y = y - 1
	end
  end

  modem.transmit(1, 1, x)
  modem.transmit(2, 2, y)  
	
  --Check at end
  turtle.select(16)
  if turtle.compareDown() then
	turtle.up()
	turtle.up()
	turtle.up()
	completed = true
	shell.run("dance")
  end
  turtle.select(1)
  print("X: "..x.."  Y: "..y)
end
Alice #2
Posted 22 February 2014 - 12:13 AM
When using elseif, you don't need to use else if (check) then (code) end else if bla bla bla
You could just use elseif (check) then (code) elseif (check) then (code)
That might help.


if true then
 print(" hai ")
else
 if false then
  print("bork")
 end
end
--vs
if true then
 print(" hai ")
elseif false then
 print("bork")
end
Edited on 21 February 2014 - 11:20 PM
DARKHAWX #3
Posted 22 February 2014 - 12:26 AM
When using elseif, you don't need to use else if (check) then (code) end else if bla bla bla
You could just use elseif (check) then (code) elseif (check) then (code)
That might help.
If I understood you correctly and changed the code correctly then that did not work. Maybe you could copy the code into your own turtle, edit it how you said to. Then test to see if it works. If it works then can you paste the code back here and let me know.
Alice #4
Posted 22 February 2014 - 12:40 AM

--Variables
local completed = false
local modem = peripheral.wrap("right")
local x = 1
local y = 1
local dir = 270
--Movement
while completed == false do
  if turtle.getFuelLevel() < 16 then
	turtle.refuel(1)
  end

  if turtle.detect() then
	turtle.turnLeft()
	dir = dir + 90
	if turtle.detect() then
	  turtle.turnRight()
	  turtle.turnRight()
	  dir = dir - 180
	  if turtle.detect() then
		turtle.turnRight()
		dir = dir - 90
		turtle.forward()
		end
	  else turtle.forward()
	  end
	else turtle.forward()
	end
  else turtle.forward()
  end
  
  --Send Coordinates
  if dir >= 360 then
	dir = dir - 360
  end
  if dir < 0 then
	dir = dir + 360
  end

  if dir == 0 then
	x = x + 1
  elseif dir == 90 then
	y = y + 1
  elseif dir == 180 then
	x = x - 1
  elseif dir == 270 then
	y = y - 1
  end

  modem.transmit(1, 1, x)
  modem.transmit(2, 2, y)  
	
  --Check at end
  turtle.select(16)
  if turtle.compareDown() then
	turtle.up()
	turtle.up()
	turtle.up()
	completed = true
	shell.run("dance")
  end
  turtle.select(1)
  print("X: "..x.."  Y: "..y)
end
c:
Edited on 21 February 2014 - 11:41 PM
DARKHAWX #5
Posted 22 February 2014 - 01:09 AM

--Variables
local completed = false
local modem = peripheral.wrap("right")
local x = 1
local y = 1
local dir = 270
--Movement
while completed == false do
  if turtle.getFuelLevel() < 16 then
	turtle.refuel(1)
  end

  if turtle.detect() then
	turtle.turnLeft()
	dir = dir + 90
	if turtle.detect() then
	  turtle.turnRight()
	  turtle.turnRight()
	  dir = dir - 180
	  if turtle.detect() then
		turtle.turnRight()
		dir = dir - 90
		turtle.forward()
		end
	  else turtle.forward()
	  end
	else turtle.forward()
	end
  else turtle.forward()
  end
  
  --Send Coordinates
  if dir >= 360 then
	dir = dir - 360
  end
  if dir < 0 then
	dir = dir + 360
  end

  if dir == 0 then
	x = x + 1
  elseif dir == 90 then
	y = y + 1
  elseif dir == 180 then
	x = x - 1
  elseif dir == 270 then
	y = y - 1
  end

  modem.transmit(1, 1, x)
  modem.transmit(2, 2, y)  
	
  --Check at end
  turtle.select(16)
  if turtle.compareDown() then
	turtle.up()
	turtle.up()
	turtle.up()
	completed = true
	shell.run("dance")
  end
  turtle.select(1)
  print("X: "..x.."  Y: "..y)
end
c:

Sorry, but it didn't work for me.
Bomb Bloke #6
Posted 22 February 2014 - 01:26 AM
  if turtle.detect() then
        turtle.turnLeft()
        dir = dir + 90
        if turtle.detect() then
          turtle.turnRight()
          turtle.turnRight()
          dir = dir - 180
          if turtle.detect() then
                turtle.turnRight()
                dir = dir - 90
                turtle.forward()
                end                  --  ** What's this doing there? **
          else turtle.forward()
          end
        else turtle.forward()
        end
  else turtle.forward()
  end
DARKHAWX #7
Posted 22 February 2014 - 06:56 AM
  if turtle.detect() then
		turtle.turnLeft()
		dir = dir + 90
		if turtle.detect() then
		  turtle.turnRight()
		  turtle.turnRight()
		  dir = dir - 180
		  if turtle.detect() then
				turtle.turnRight()
				dir = dir - 90
				turtle.forward()
				end				  --  ** What's this doing there? **
		  else turtle.forward()
		  end
		else turtle.forward()
		end
  else turtle.forward()
  end
thanks, that worked.