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

[LUA][Error] 'end' expected

Started by Mackan90096, 22 March 2013 - 05:56 AM
Mackan90096 #1
Posted 22 March 2013 - 06:56 AM
Hi!
I am trying to make a ordering script that will send the orders to my email which i have setup.

But i keep getting errors! :(/>

Please help me.

Script:
Spoiler


function start()
rednet.open("back")
term.clear()
term.setCursorPos(1,1)
write("What cart to order?: ")
local input = read()
if input == not "1" then
fail()
end
elseif input == not "2" then
fail()
local function fail()
term.clear()
print("Failed!")
sleep(1)
start()
end
elseif input == "1" then
term.clear()
term.setCursorPos(1,1)
print("Ordering")
http.post(
"http://computercraft.comli.com/email.php?message="..textutils.urlEncode(tostring(input))
)
sleep(1)
if http_failure then
print("Failed")
else
print("Success!")
sleep(1)
term.clear()
orderid()
end
elseif input == "2" then
term.clear()
term.setCursorPos(1,1)
print("Ordering")
http.post(
"http://computercraft.comli.com/email.php?message="..textutils.urlEncode(tostring(input))
)
sleep(1)
if http_failure then
print("Failed")
end
else
print("Success!")
sleep(1)
term.clear()
orderid()
else
start()
end
end
end
end
end


function orderid()
orderid = math.random(1,99999999)
term.clear()
term.setCursorPos(1,1)
print("Your order number is: "..orderid)
rednet.send(ordercomp, orderid)
sleep(15)
term.clear()
start()
end

start()
OmegaVest #2
Posted 22 March 2013 - 07:09 AM
I think the biggest problem you have is incorrect if-block grammar.

It should go as such:


if [condition] then
  -- Body
elseif [condition] then
  -- Body
else
  -- Body
end

Also, it may help to format your code so that every time you open a conditional statement, you indent, and then when you close one (like an end or the next case), you de-indent. ie

function func(dark)
  while [cond] do
    if dark then
      --something
    else
      --somethingelse
    end
  end
end


It helps to find missing or extra ends.
faubiguy #3
Posted 22 March 2013 - 07:09 AM
I fixed some errors I found and commented what they were (Including the end expected). I can't test this right now, but It looks right.

Spoiler
function start()
  local function fail() -- moved to top so that its defined before you try to call it.
    term.clear()
    print("Failed!")
    sleep(1)
    return start() -- Made calls to start, fail, and orderid into tail calls to avoid stack overflow from recursion.
  end

  rednet.open("back")
  term.clear()
  term.setCursorPos(1,1)
  write("What cart to order?: ")
  local input = read()
  if input ~= "1" and input ~= "2" then -- changed == not to ~=, combined statements for "1" and "2" so that it won't fail if the input isn't "1" and "2" at the same time
    return fail()
  elseif input == "1" then -- You may want to marge this with the elseif input == "2", because they have the same content
    term.clear()
    term.setCursorPos(1,1)
    print("Ordering")
    http.post(
      "http://computercraft.comli.com/email.php?message="..textutils.urlEncode(tostring(input))
    )
    sleep(1)
    if http_failure then
      print("Failed")
    else
      print("Success!")
      sleep(1)
      term.clear()
      return orderid()
    end
    -- Removed end statements between elseif blocks. That was the cause of the posted error.
  elseif input == "2" then
    term.clear()
    term.setCursorPos(1,1)
    print("Ordering")
    http.post(
      "http://computercraft.comli.com/email.php?message="..textutils.urlEncode(tostring(input))
    )
    sleep(1)
    if http_failure then
      print("Failed")
    else
      print("Success!")
      sleep(1)
      term.clear()
      return orderid()
    end
  else -- This block will never be reached, because if the input is "1" or "2" then it will order, and if not it will fail.
    return start()
  end
end


function orderid()
  local orderid = math.random(1,99999999) --made local to avoid overwriting orderid function
  term.clear()
  term.setCursorPos(1,1)
  print("Your order number is: "..orderid)
  rednet.send(ordercomp, orderid)
  sleep(15)
  term.clear()
  return start()
end

start()

EDIT: There was indentation, but the forum software removed it.
EDIT 2: Went back and added indentation again.
Mackan90096 #4
Posted 22 March 2013 - 07:22 AM
Ah, thanks!
Mackan90096 #5
Posted 22 March 2013 - 07:26 AM
I fixed some errors I found and commented what they were (Including the end expected). I can't test this right now, but It looks right.

Spoiler
function start()
local function fail() -- moved to top so that its defined before you try to call it.
term.clear()
print("Failed!")
sleep(1)
return start() -- Made calls to start, fail, and orderid into tail calls to avoid stack overflow from recursion.
end

rednet.open("back")
term.clear()
term.setCursorPos(1,1)
write("What cart to order?: ")
local input = read()
if input ~= "1" and input ~= "2" then -- changed == not to ~=, combined statements for "1" and "2" so that it won't fail if the input isn't "1" and "2" at the same time
return fail()
elseif input == "1" then
term.clear()
term.setCursorPos(1,1)
print("Ordering")
http.post(
"http://computercraft.comli.com/email.php?message="..textutils.urlEncode(tostring(input))
)
sleep(1)
if http_failure then
print("Failed")
else
print("Success!")
sleep(1)
term.clear()
return orderid()
end
-- Removed end statements between elseif blocks. That was the cause of the posted error.
elseif input == "2" then
term.clear()
term.setCursorPos(1,1)
print("Ordering")
http.post(
"http://computercraft.comli.com/email.php?message="..textutils.urlEncode(tostring(input))
)
sleep(1)
if http_failure then
print("Failed")
else
print("Success!")
sleep(1)
term.clear()
return orderid()
end
else
end
return start()
end


function orderid()
local orderid = math.random(1,99999999) --made local to avoid overwriting orderid function
term.clear()
term.setCursorPos(1,1)
print("Your order number is: "..orderid)
rednet.send(ordercomp, orderid)
sleep(15)
term.clear()
return start()
end

start()

EDIT: There was indentation, but the forum software removed it.

You might also be

I tried that but i get 'end' expected to close if at line 14
OmegaVest #6
Posted 22 March 2013 - 07:35 AM
Okay, so I reformatted faubiguy's code, and here is what I got, let's see if you catch the problem.
Spoiler


function start()
  local function fail() -- moved to top so that its defined before you try to call it.
	term.clear()
	print("Failed!")
	sleep(1)
	return start() -- Made calls to start, fail, and orderid into tail calls to avoid stack overflow from recursion.
  end

  rednet.open("back")
  term.clear()
  term.setCursorPos(1,1)
  write("What cart to order?: ")
  local input = read()
  if input ~= "1" and input ~= "2" then -- changed == not to ~=, combined statements for "1" and "2" so that it won't fail if the input isn't "1" and "2" at the same time
	return fail()
  elseif input == "1" then
	term.clear()
	term.setCursorPos(1,1)
	print("Ordering")
	http.post(
	  "http://computercraft.comli.com/email.php?message="..textutils.urlEncode(tostring(input))
	)
	sleep(1)
	if http_failure then
	  print("Failed")
	else
	  print("Success!")
	  sleep(1)
	  term.clear()
	  return orderid()
	end
	-- Removed end statements between elseif blocks. That was the cause of the posted error.
  elseif input == "2" then
	term.clear()
	term.setCursorPos(1,1)
	print("Ordering")
	http.post(
	  "http://computercraft.comli.com/email.php?message="..textutils.urlEncode(tostring(input))
	)
	sleep(1)
	if http_failure then
	  print("Failed")
	else
	  print("Success!")
	  sleep(1)
	  term.clear()
	  return orderid()
	end
  else
  end
  return start()
end


function orderid()
  local orderid = math.random(1,99999999) --made local to avoid overwriting orderid function
  term.clear()
  term.setCursorPos(1,1)
  print("Your order number is: "..orderid)
  rednet.send(ordercomp, orderid)
  sleep(15)
  term.clear()
  return start()
end

start()



EDIT: I changed some of my own errors in formatting, make sure you are commenting on the corrected version. Also, the forum code tags do some weird things with spacing, I'll grant that.
remiX #7
Posted 22 March 2013 - 07:36 AM
Faubiguy, when pasting code, click "Editing mode" before (I think it's that - it's the top left icon in the new post section).
Doing that will keep the indentation.

I have to do this for Chrome, but not firefox. Not sure about other browsers
Mackan90096 #8
Posted 22 March 2013 - 07:42 AM
Okay, so I reformatted faubiguy's code, and here is what I got, let's see if you catch the problem.
Spoiler


function start()
  local function fail() -- moved to top so that its defined before you try to call it.
	term.clear()
	print("Failed!")
	sleep(1)
	return start() -- Made calls to start, fail, and orderid into tail calls to avoid stack overflow from recursion.
  end

  rednet.open("back")
  term.clear()
  term.setCursorPos(1,1)
  write("What cart to order?: ")
  local input = read()
  if input ~= "1" and input ~= "2" then -- changed == not to ~=, combined statements for "1" and "2" so that it won't fail if the input isn't "1" and "2" at the same time
	return fail()
  elseif input == "1" then
	term.clear()
	term.setCursorPos(1,1)
	print("Ordering")
	http.post(
	  "http://computercraft.comli.com/email.php?message="..textutils.urlEncode(tostring(input))
	)
	sleep(1)
	if http_failure then
	  print("Failed")
	else
	  print("Success!")
	  sleep(1)
	  term.clear()
	  return orderid()
	end
	-- Removed end statements between elseif blocks. That was the cause of the posted error.
  elseif input == "2" then
	term.clear()
	term.setCursorPos(1,1)
	print("Ordering")
	http.post(
	  "http://computercraft.comli.com/email.php?message="..textutils.urlEncode(tostring(input))
	)
	sleep(1)
	if http_failure then
	  print("Failed")
	else
	  print("Success!")
	  sleep(1)
	  term.clear()
	  return orderid()
	end
  else
  end
  return start()
end


function orderid()
  local orderid = math.random(1,99999999) --made local to avoid overwriting orderid function
  term.clear()
  term.setCursorPos(1,1)
  print("Your order number is: "..orderid)
  rednet.send(ordercomp, orderid)
  sleep(15)
  term.clear()
  return start()
end

start()



EDIT: I changed some of my own errors in formatting, make sure you are commenting on the corrected version. Also, the forum code tags do some weird things with spacing, I'll grant that.

This worked! THANKS!
Mackan90096 #9
Posted 22 March 2013 - 08:00 AM
Can i make this send the input and the orderid?

Edit: Nvm