Jump to content




[Error] [lua] Eof expected?


28 replies to this topic

#21 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,454 posts

Posted 15 June 2012 - 05:11 PM

Please post real code that's actually generating errors in the future. The end statement ends a block of code:

function name()
    --code
end

while true do
    --code
end

for i=1,42 do
    --code
end

if x then
    --code
end

if x then
    --code
else
    --code
end

if x then
    --code
elseif y then
    --code
else
    --code
end

The general rule of thumb is, every function, for, while and if needs a matching end. Note that the entire if/elseif/else block is between the if and its matching end.

#22 deity12

  • New Members
  • 19 posts

Posted 16 June 2012 - 06:30 AM

Ok, will try thanks, sorry for not posting the right code.

#23 deity12

  • New Members
  • 19 posts

Posted 16 June 2012 - 07:26 AM

Ok, right so, I've followed what you said and fixed up a program I wrote, but now it just runs straight through, without doin a thing, presumably it's somehow hitting an end before it does anything but I don't see how. Could someone please help?
function turtlecom()
shell.run("clear")
print ("Turtlecom Running.")
print ("Write 'help' for help or 'exit' to exit")
x = io.read()
if x == "help" then
print ("f = move forward")
print ("b = move back")
print ("l = turn left")
print ("r = turn right")
print ("Will return to Turtlecom interface in 5 seconds")
sleep(5)
turtlecom()
elseif x == "f" then
turtle.forward()
turtlecom()
elseif x == "b" then
turtle.back()
turtlecom()
elseif x == "l" then
turtle.turnLeft()
turtlecom()
elseif x == "r" then
turtle.turnRight()
turtlecom()
elseif x == "exit" then
print ("Closing turtlecom ...")
sleep(1)
else
print ("That is not a valid command.")
turtlecom()
end
end

Nevermind got it myself. But i would like to know how the os.pullevent() works so i can use it to improve my program (so it's key presses rather than key press and then enter.)

#24 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,454 posts

Posted 16 June 2012 - 08:43 AM

By the way, it's very bad practice to call a function from itself in order to create an infinite loop. Use this instead:

while true do
    term.clear()
    term.setCursorPos(1,1)
    print ("Turtlecom Running.")
    print ("Write 'help' for help or 'exit' to exit")
    x = read()
    if x == "help" then
        print ("f = move forward")
        print ("b = move back")
        print ("l = turn left")
        print ("r = turn right")
        print ("Will return to Turtlecom interface in 5 seconds")
        sleep(5)
    elseif x == "f" then
        turtle.forward()
    elseif x == "b" then
        turtle.back()
    elseif x == "l" then
        turtle.turnLeft()
    elseif x == "r" then
        turtle.turnRight()
    elseif x == "exit" then
        print ("Closing turtlecom ...")
        sleep(1)
        break
    else
        print ("That is not a valid command.")
    end
end


#25 deity12

  • New Members
  • 19 posts

Posted 16 June 2012 - 08:57 AM

View PostLyqyd, on 16 June 2012 - 08:43 AM, said:

By the way, it's very bad practice to call a function from itself in order to create an infinite loop. Use this instead:

while true do
	term.clear()
	term.setCursorPos(1,1)
	print ("Turtlecom Running.")
	print ("Write 'help' for help or 'exit' to exit")
	x = read()
	if x == "help" then
		print ("f = move forward")
		print ("b = move back")
		print ("l = turn left")
		print ("r = turn right")
		print ("Will return to Turtlecom interface in 5 seconds")
		sleep(5)
	elseif x == "f" then
		turtle.forward()
	elseif x == "b" then
		turtle.back()
	elseif x == "l" then
		turtle.turnLeft()
	elseif x == "r" then
		turtle.turnRight()
	elseif x == "exit" then
		print ("Closing turtlecom ...")
		sleep(1)
		break
	else
		print ("That is not a valid command.")
	end
end

Thanks for the hint. That's much easier to code! I knew of the 'while true do' but never thought of using it like that for some reason.
Also what is the diff between io.read() and read()? or is there none?
Also still looking for an answer to the os.pullevent() question (2 posts up from here)
There are many more features I want to add to this program but the amount of them clogs up the help screen and I need a "next" button. Also, it would be immensly helpful so I don;t have to press enter after every command.

#26 deity12

  • New Members
  • 19 posts

Posted 17 June 2012 - 12:23 AM

Might create a new thread for the os.pull as this one seems to have died...

#27 Pinkishu

  • Members
  • 484 posts

Posted 17 June 2012 - 10:31 AM

http://www.computerc...w-is-it-useful/
theres a tutorial about that :(/>

#28 mara_12

  • Members
  • 1 posts

Posted 21 January 2013 - 02:10 AM

I have the same problem on the string: else . I don't know whats that -_-

#29 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,454 posts

Posted 21 January 2013 - 05:31 AM

View Postmara_12, on 21 January 2013 - 02:10 AM, said:

I have the same problem on the string: else . I don't know whats that -_-

Please create a new topic with the code you're using and the full error message you're receiving.

Locked.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users