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

Error bioss:366 'end' expected in my security program please help!

Started by Maxwell0156, 17 August 2014 - 08:23 PM
Maxwell0156 #1
Posted 17 August 2014 - 10:23 PM
The Error I am Getting is bios:366: [string "startup"]:12: 'end'expected (to close 'if' at line 10)
I have looked at the code for many hours and can't figure it out please help! And yes I know that the error means that there's not enough block ends but I've looked and looked and I think I should be getting a eof not an end error. If you see any other ways to fix any bugs or improve the code please tell me I've been using cumputer craft to learn about lua so I would love it if you could help with that to thanks :)/>.


--Variables
pass = "12345"
maxFails = "3"
numFails = "0"

--Functions

  --check Passcode
function checkPasscode()
  if io.read == pass then
	return true
	numFails = 0
  else
	return false
	numFails = numFails + 1
	end
  end
end

  --Check Fails
function checkFails()
  if checkPasscode() == false then
	if numFails == maxFails then
	  return true
	else if numFails > maxFails then
	  return true
  elseif checkPasscode() == true then
	return false
  else
	return false
	end
  end
end

  --Fails Seter
function setFails()

  if checkFails() == true then
	if checkPasscode() false then
	numFails = numFails + 1
	return true

  elseif check Fails() == false then
	numFails = 0
	return true

  else
	return false

--clear
function clear()
  term.clear
  term.setCursorPos(1,1)
end
--Looped Code
while true do
clear()

print("										  ")
print("		   Enter Your Passcode.		   ")
print("				  o-----o				 ")
write("				  |	 |				 ")
print("				  o-----o				 ")
term.setCursorPos(28,4)
if checkPasscode() == true then
  setFails()
  term.clear
  term.setCursorPos(1,2)
  io.read("*")
  print("		   o-----------------o")
  print("		   |Correct Passcode!|")
  print("		   |				 |")
  print("		   | Access Granted! |")
  print("		   o-----------------o")
  sleep(3)
  term.clear
  io.read()
  print("		 o----------------------o")
  print("		 |	Security  Mode	|")
  print("		 o----------------------o")
  print("		 |Press 1 to Activate   |")
  print("		 |Prees 2 to DeActivate |")
  print("		 o----------------------o")
  if io.read == 1 then
	term.clear
	print("			   Activated")
	rs.setOutput("back",true)
	sleep(15)
  else if io.read == 2 then
	term.clear
	print("			  DeActivated")
	rs.setOutput("back",false)
	sleep(4)
  else
	term.clear
	print("			 Unknown Input")
	sleep(3)
	term.clear
else if checkPasscode == false then
  setFails()
  print("			  Wrong  passcode")
  sleep(3)
  if checkFails == true then
	setFails()
	print("   To Many Fails Kicked from system for 30 seconds.")
	sleep(30.2)
	term.clear
  else if checkFails == false then
	print("					 Try Agin")
	sleep(5)
  term.clear
	end
  end
end
Edited on 18 August 2014 - 01:10 AM
GamerNebulae #2
Posted 17 August 2014 - 10:59 PM
first of all, you put your code in the
 
brackets. The error means you forgot an end somewhere. I would suggest using indentations in your code and you would see in in no-time.
RickiHN #3
Posted 17 August 2014 - 11:14 PM

--check Passcode
function checkPasscode()
   if io.read == pass then
	  return true
	  numFails = 0
   else
	  return false
	  numFails = numFails + 1
   end
end
end <----????
theoriginalbit #4
Posted 18 August 2014 - 03:19 AM
Additionally to what RickiHN pointed out with your extra end, it should also be noted that you have a statement after the return false, in the checkPasscode function, this is in particular what it is complaining about.

Additionally you'll receive an error along the lines of = expected due to the fact that you're never calling the term.clear function, it should be term.clear() which interestingly enough you have actually defined a function clear but never use it.

If statements should be formatted like so

if condition then
  --# code if true
end

if condition then
  --# code if true
else
  --# code if false
end

if condition1 then
  --# if condition1 is true
elseif condition2 then
  --# if condition1 is false and condition2 is true
end

if condition1 then
  --# if condition1 is true
elseif condition2 then
  --# if condition1 is false and condition2 is true
else
  --# if both condition1 and condition2 are false
end
making use of these structures will reduce your problem with missing ends in the future.
Dragon53535 #5
Posted 18 August 2014 - 04:36 AM

--Variables
pass = "12345"
maxFails = "3"
numFails = "0"
Your code is off here.
There are 5 basic kinds of things in lua, they are the fundamental parts and can make the simplest programs run or the most complicated programs run.
They are as such; Operators, Variables, Strings, Numbers, and Functions.

Operators:
SpoilerAn Operator is something like IF, FOR, WHILE, REPEAT, and so on, they make the program think and change paths. However after every Operator you need to end it with well, an end. However you can have operators inside of other operators such as:

if 1 == 1 then
  if 2 == 2 then
	print("Yay Math!")
However just trying this code would not work and you would get the problem you already had, computercraft is expecting an end. so if you were to just add end end onto the bottom it would look like this:

if 1 == 1 then
  if 2 == 2 then
	print("Yay Math!")
  end
end
Do you notice how the ends line up with the if's? That means that there's no more ends that you need, and that you also don't have too many.

Variables:
SpoilerVariables are things that stand for other things, which are pass, maxFails, and numFails in your situation, however they exist in two states, local and global. A local variable can be only used by the things in the same area, be that an IF statement, or a function or even at the top of your program like these (Which would work for the entire program :P/>). Global variables however do not have the same limitations. A global variable, like the three you have there, can be used anywhere, even outside of your program. So if you were to make a new program and put the global variable Hello = "Hi there" and printed Hello in a different program, it would say Hi there. A more in depth tutorial about code blocks and variable scopes can be found easily here.

Strings:
SpoilerStrings are basically text you can use, and in your case "12345","3", and "0" are all strings, however you cannot apply math to a string. Every string must have quotes around them, or they are not strings " <– these things. Now strings are important as they are text that Lua says, oh yeah this isn't a variable I know what this means. So if you were to type print(lol) you would not print anything as lol is not a defined variable and thus is nil, however if you were to type print("lol") then lol would pop up on screen as print() know's what to put up.

Numbers:
SpoilerNumbers are things that you can actually do math to and manipulate at will with addition and subtraction and such. Numbers are just well, numbers without the quotes around them so 12345, 3, 0 are all numbers. In your case here you actually want the "3" and "0" to be numbers so just remove the quotes. You can of course create variables with the variable as a number, however I would not recommend it. As well as you can do the opposite and make a variable equal a number like you're trying to do with maxFails. You can convert a string into a number and a number into a string by using:

tonumber("1") -- this would in essence remove the quotes around the 1 and make it a number and allow you to use math
--# And you can turn a number into a string using
tostring(1) --  this would in essence add quotes around the 1 and make it a string "1"

Functions:
SpoilerFunctions are blocks of code that you can use over and over again. Every function MUST have parenthesis (() <–These things) for it to be considered a function by the program. Many things you use are already made functions such as print(), read() and term.clear(). These are things made by the computer shell and are on every computer. A way to make functions of your own is as easy as saying:

function Main()
--# Some code here
end
Like operators, you need to close off your functions with an end. However like variables you can also make them local and you must always define the function before you can use it so putting down:

Main()
function Main()
--#Some code here
end
Would not work as at the point in the code the program doesn't know what Main() is and would error. Functions are fairly useful in providing easy access to frequently used code. Functions are able to be sent data from other things such as:

function Main(Variable1, Variable2)
  print(Variable1)
end
Main("Hello")
In this case the function Main basically has nothing inside of it, and if it was just called as Main() would not print anything, however since we use Main("Hello") to call it then we are really just using:

function Main(Variable1 = "Hello",Variable2)
  print(Variable1) --This would print Hello
end
So there you go, the 5 most basic things lua uses. Now you should know enough to know that you should change "3" and "0" to 3 and 0.
Edited on 18 August 2014 - 02:54 AM
Dragon53535 #6
Posted 19 August 2014 - 12:09 AM
function checkPasscode()
  if io.read == pass then
		return true
		numFails = 0
  else
		return false
		numFails = numFails + 1
  end
end
end
There are some things here you should work on and know for future reference. Firstly, io.read is a function and as such much have parenthesis (). So that line should be
if io.read() == pass then
Secondly inside of that if, you're returning true, now return works in the sense that it's leaving the function right then and there, and as such anything after that is negligible. This is where your error is as the computer is seeing it as

if io.read == pass then
  return true
numFails = 0
else
And so the program thinks that since it's returning, and the next thing isn't an else or elseif, then the if must be done and needs an end. the easy way to fix that is to just put numFails = 0 before the return. You should also do that inside of the else as well.

Finally to finish off that function, you have one too many ends at the end which you can see very easily with proper indentation. A great place to start learning proper indentation is a tutorial made by robhol here. It's super simple and it shows how much it helps to indent.
Edited on 18 August 2014 - 10:14 PM