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

Expecting END error, add end, <eof> error. Help.

Started by FallenMoons, 14 May 2015 - 12:55 AM
FallenMoons #1
Posted 14 May 2015 - 02:55 AM
Not sure why but it's giving me this error… I've read it over at least 20 times scratching my head.

ERROR: bios:366: [string "berry"]:181: 'end' expected (to close 'while' at line 162)

Spoiler

local x = 0 --home x of the turtle
local z = 0 --home z of the turtle
local dir = "x-" --x+,z-,z+
local fx = 23
local fz = 4
local harvesting = false
function up()
  turtle.up()
end
function down()
turtle.down()
end
function forward()
  turtle.forward()
    if dir=="x-" then
	  x=x-1
    end

if dir=="x+" then
	    x=x+1
    end

if dir=="z-" then
  z=z-1
    end

if dir=="z+" then
	    z=z+1
    end
end
function tr()
turtle.turnRight()
   
if dir=="x-" then
   dir="z-"
    end

if dir=="x+" then
	    dir="z+"
    end

if dir=="z-" then
  dir=x"+"
    end

if dir=="z+" then
  dir="x-"
end
end
function tl()
    turtle.turnLeft()
   
if dir=="x-" then
   dir="z+"
    end

if dir=="x+" then
	    dir="z-"
    end

if dir=="z-" then
  dir="x-"
    end

if dir=="z+" then
  dir="x+"
end
end
function back()
turtle.back()
if dir=="x-" then
	  x=x+1
    end

if dir=="x+" then
	    x=x-1
    end

if dir=="z-" then
	    z=z+1
    end

if dir=="z+" then
	    z=z-1
    end
end
function h()
local g = getGrowthDown()
if g==100 then
  r=turtle.place()
  if r==false then
  turtle.digDown()
  turtle.suckDown()
  turtle.select(1)
  turtle.place()
  else
  turtle.suckDown()
  end
end
end
function start()
turtle.up()
sleep(1)
turtle.forward()
sleep(1)
harvesting=true
end
function goHome()
repeat
  if dir=="x-" then
   if x>0 then
    forward()
   end
  end
 
  if dir=="x+" then
   if x>0 then
    backward()
   end
  end
 
  if dir=="z+" then
   if z>0 then
    forward()
   end
  end
 
  if dir=="z-" then
   if z>0 then
    forward()
   end
  end
until x==0 and y==0
end
repeat
io.write("Should I start? [y/n]")
yn=io.read()
io.clear()
until yn=="y" or "n"
if yn=="y" then
harvesting=true
start()
end
if yn=="n" then
io.write("Exiting")
sleep(1)
io.reboot()
end
while harvesting==true do
h()
if z > fz then
  goHome()
end

if x~=fx and x > 0 then
   forward()
else
   tr()
   forward()
   tr()
end
if x==0 then
  tl()
  forward()
  tl()
end
until harvesting==false
KingofGamesYami #2
Posted 14 May 2015 - 03:05 AM

local x = 0 --home x of the turtle
local z = 0 --home z of the turtle
local dir = "x-" --x+,z-,z+
local fx = 23
local fz = 4
local harvesting = false
function up()
  turtle.up()
end
function down()
  turtle.down()
end
function forward()
  turtle.forward()
  if dir=="x-" then
    x=x-1
  end

  if dir=="x+" then
    x=x+1
  end

  if dir=="z-" then
    z=z-1
  end

  if dir=="z+" then
    z=z+1
  end
end
function tr()
  turtle.turnRight()

  if dir=="x-" then
    dir="z-"
  end

  if dir=="x+" then
    dir="z+"
  end

  if dir=="z-" then
    dir=x"+"
  end

  if dir=="z+" then
    dir="x-"
  end
end
function tl()
  turtle.turnLeft()

  if dir=="x-" then
    dir="z+"
  end

  if dir=="x+" then
    dir="z-"
  end

  if dir=="z-" then
    dir="x-"
  end

  if dir=="z+" then
    dir="x+"
  end
end
function back()
  turtle.back()
  if dir=="x-" then
    x=x+1
  end

  if dir=="x+" then
    x=x-1
  end

  if dir=="z-" then
    z=z+1
  end

  if dir=="z+" then
    z=z-1
  end
end
function h()
  local g = getGrowthDown()
  if g==100 then
    r=turtle.place()
    if r==false then
      turtle.digDown()
      turtle.suckDown()
      turtle.select(1)
      turtle.place()
    else
      turtle.suckDown()
    end
  end
end
function start()
  turtle.up()
  sleep(1)
  turtle.forward()
  sleep(1)
  harvesting=true
end
function goHome()
  repeat
    if dir=="x-" then
      if x>0 then
        forward()
      end
    end

    if dir=="x+" then
      if x>0 then
        backward()
      end
    end

    if dir=="z+" then
      if z>0 then
        forward()
      end
    end

    if dir=="z-" then
      if z>0 then
        forward()
      end
    end
  until x==0 and y==0
end
repeat
  io.write("Should I start? [y/n]")
  yn=io.read()
  io.clear()
until yn=="y" or "n"
if yn=="y" then
  harvesting=true
  start()
end
if yn=="n" then
  io.write("Exiting")
  sleep(1)
  io.reboot()
end
while harvesting==true do
  h()
  if z > fz then
    goHome()
  end

  if x~=fx and x > 0 then
    forward()
  else
    tr()
    forward()
    tr()
  end
  if x==0 then
    tl()
    forward()
    tl()
  end
  until harvesting==false

I think you meant to use an end instead of until.
Edited on 14 May 2015 - 01:09 AM
FallenMoons #3
Posted 14 May 2015 - 03:32 AM
Yup. Brain didnt work. lol Thanks!
Dragon53535 #4
Posted 14 May 2015 - 05:03 PM
I've got a few tips for you.
1:

until yn=="y" or "n"
This will not work, it will ALWAYS exit the loop even if yn is "1230981025918059815"
Or doesn't work like that, it won't let you compare if yn is a or b, you have to say if yn is a or yn is b like so

until yn=="y" or yn == "n"

2: Your massive amount of if end statements can be shortened by if elseifs

function goHome()
  repeat
    if dir=="x-" then
	  if x>0 then
	    forward()
	  end
    end
    if dir=="x+" then
	  if x>0 then
	    backward()
	  end
    end
    if dir=="z+" then
	  if z>0 then
	    forward()
	  end
    end
    if dir=="z-" then
	  if z>0 then
	    forward()
	  end
    end
  until x==0 and y==0
end
Can be easily shortened to

function goHome()
  repeat
    if dir=="x-" then
	  if x>0 then
	    forward()
	  end
    elseif dir=="x+" then
	  if x>0 then
	    backward()
	  end
    elseif dir=="z+" then
	  if z>0 then
	    forward()
	  end
    elseif dir=="z-" then
	  if z>0 then
	    forward()
	  end
    end
  until x==0 and y==0
end
Notice how I got rid of having to remember like 4 ends there? If you notice, the ends that were on the separate if statements are now gone, that's because an if elseif statement just needs one end at the end of an if elseif chain.
FallenMoons #5
Posted 15 May 2015 - 01:11 AM
Thanks! I was originally using elseif's but it kept giving me errors and frankly I just wanted to get the script to work. Once i get things to work I usually go back and optimize them. Unless I'm lazy (which is often)! :D/>

I am new to the Lua language and have only previously worked with GML (game maker language, it got me into programming) and Java. Both of which the "until yn=="n" or "y" would work. Thanks again!
Dragon53535 #6
Posted 15 May 2015 - 01:46 AM
I am new to the Lua language and have only previously worked with GML (game maker language, it got me into programming) and Java. Both of which the "until yn=="n" or "y" would work. Thanks again!
Yeah no, it won't work. I just tested in GM:S and it does not work. If need be I'll open up Eclipse and have it yell at me that it won't work. (Also, from a personal standpoint, GML is pretty terrible.)
flaghacker #7
Posted 15 May 2015 - 07:55 AM
I am new to the Lua language and have only previously worked with GML (game maker language, it got me into programming) and Java. Both of which the "until yn=="n" or "y" would work. Thanks again!
Yeah no, it won't work. I just tested in GM:S and it does not work. If need be I'll open up Eclipse and have it yell at me that it won't work. (Also, from a personal standpoint, GML is pretty terrible.)

I'm pretty sure that doesn't work in Java too.