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

Having An Error 'then' Expected

Started by bjohnston562, 31 July 2013 - 11:06 PM
bjohnston562 #1
Posted 01 August 2013 - 01:06 AM
I am having this error
bios:338: [string "1"] : 13 : 'then' expected

so I have a then at line 13 but I still get this error.

http://pastebin.com/3DkH4mkx



the function is above
it is supposed to find out if the inventory has materials to build with and if it doesnt i want it to wait for me to fill it up and then type 1.
campicus #2
Posted 01 August 2013 - 01:14 AM
if pass == 1

You seriously need to format your code properly…

EDIT: You also need to define s at the start of your program:

local s = 1


local s = 1

function check()
  if turtle.getItemCount(s) == 0 then
    s = s + 1
    if s < 17 then
	  turtle.select(s)
    else
	  while true do
	    print("Waiting for supplies. Type 1 to return to building.")
	    s = read()
	    if s == 1 then
		  break()
	    end
	  end
	  turtle.select(s)
    end
  end
end
bjohnston562 #3
Posted 01 August 2013 - 01:16 AM
sorry about that. It did not get copied pasted over correctly. I will just type it out next time. Thank you by the way :)/>

edit:
Thank you again for the help. I will define s as a local variable. :)/>
campicus #4
Posted 01 August 2013 - 01:25 AM
Glad to help

Also, use a repeat loops instead of 'break'


repeat
  print("YourTextHere")
  s = read()
  sleep(0.1)
until s == 1
bjohnston562 #5
Posted 01 August 2013 - 01:29 AM
o that is great I was wondering if there was a function like that in lua. I am really just starting out so the repeat function would be great. thank you guys so much this is great!
theoriginalbit #6
Posted 01 August 2013 - 01:30 AM
There is one problem that you'll have in the example campicus gave, and that is that `break` is not a function, it is a keyword that upon using immediately exits the loop the program is within.

Also bjohnston562 for next time, please use [code][/code] tags around your code to have them nicely formatted too, it makes the code much easier to read.

As which loop to use, it is personal preference, you can use the `while` loop or the `repeat` loop that campicus showed, personally I commonly use the `while` loop.

EDIT: If you're just getting started with Lua, might I suggest you have a read of the Programming in Lua manual, created by the developers of Lua (found here), also the Lua-users tutorials can be quite useful (most are easier to find by Googling "Lua topic tutorial"
bjohnston562 #7
Posted 01 August 2013 - 01:46 AM
I will check that developers manual. I didn't know it existed. I watched direwolf 20's youtube videos, but those were just the basics.
theoriginalbit #8
Posted 01 August 2013 - 02:39 AM
I will check that developers manual. I didn't know it existed. I watched direwolf 20's youtube videos, but those were just the basics.
It is probably best if you don't follow coding styles of Direwolf20, his videos are great to watch and all, but he really isn't the best programmer, making many mistakes in programs, having very large over complicated programs, and very bad coding practices/habits. To give one example his button API is ~90 lines of code… it could be redone in ~20 lines by most of the users in this community.
GamerNebulae #9
Posted 01 August 2013 - 05:31 AM
I will check that developers manual. I didn't know it existed. I watched direwolf 20's youtube videos, but those were just the basics.
It is probably best if you don't follow coding styles of Direwolf20, his videos are great to watch and all, but he really isn't the best programmer, making many mistakes in programs, having very large over complicated programs, and very bad coding practices/habits. To give one example his button API is ~90 lines of code… it could be redone in ~20 lines by most of the users in this community.

That's exactly what I thought when I saw his button API. The references all over the places to different functions confused me so much… I saw you also commented on my thread about the multi-dimensional array. It's almost done, but it doesn't have the blink function yet, also options for rednet messages and stuff are missing yet. But those are functions for later.
campicus #10
Posted 01 August 2013 - 05:45 AM
Oops! Yep I should have used 'break' not 'break()' sorry! Thanks for picking that up :)/>

As for which loop to use, follow the rule of parsimony (simplicity is best!). The repeat loop here is 'better' because its shorter, imo.

That said, I'm anything but a lua master lol