9 posts
Posted 17 August 2013 - 06:38 PM
When I run this program, which is supposed to open and close a gate on command, I get this error:
bios:338: [string "Power"]:52: 'end' expected (to close 'if' at line 9)
Any help would be nice!
Program:
os.pullEvent = os.pullEventRaw
print ("Welcome, what would you like to do?")
print ("1: Open")
print ("2: Close")
write("")
password = io.read()
if password == "1" then
print ("Opening Gate")
rs.setOutput ("back", true )
sleep(1)
rs.setOutput ("back", false )
sleep(1)
rs.setOutput ("back", true )
sleep(1)
rs.setOutput ("back", false )
sleep(1)
rs.setOutput ("back", true )
sleep(1)
rs.setOutput ("back", false )
sleep(1)
rs.setOutput ("back", true )
sleep(1)
rs.setOutput ("back", false )
else
if password == "2" then
print ("Closing Gate")
rs.setOutput ("top", true )
sleep(1)
rs.setOutput ("top", false )
sleep(1)
rs.seyOutput ("top", true )
sleep(1)
rs.setOutput ("top", false )
sleep(1)
rs.setOutput ("top", true )
sleep(1)
rs.setOutput ("top", false )
sleep(1)
rs.setOutput ("top", true )
sleep(1)
rs.setOutput ("top", false )
else
print ("Sorry, not a correct input.")
print ("Please try again!")
sleep(2)
os.shutdown()
end
1852 posts
Location
Sweden
Posted 17 August 2013 - 07:09 PM
You should use 'elseif' statements and 'for' loops
input = read()
if input == "Hello" then
print("Hello World!")
elseif input == "Bye" then
print("Bye! See you later!")
else --If it's not equal to any of those above
print("What are you talking about?")
end
for i = 1,8 do
rs.setOutput("top",true)
sleep(1)
rs.setOutput("top",false)
sleep(1)
end
But just change if password == "2" to elseif password == "2" and remove else above it and it should work ;)/>
9 posts
Posted 17 August 2013 - 07:12 PM
I'm currently trying to make a program that can open/close a frame motor gate. As seen in the program, if you type 1, the gate should open, and if you type 2, the gate should close, if none of those are selected, it tells you its wrong…the problem is I keep getting the error:
bios:338: [string "Power"]:52: 'end' expected (to close 'if' at line 9) How can I fix this…heres the whole program code:
os.pullEvent = os.pullEventRaw
print ("Welcome, what would you like to do?")
print ("1: Open")
print ("2: Close")
write("")
password = io.read()
if password == "1" then
print ("Opening Gate")
rs.setOutput ("back", true )
sleep(1)
rs.setOutput ("back", false )
sleep(1)
rs.setOutput ("back", true )
sleep(1)
rs.setOutput ("back", false )
sleep(1)
rs.setOutput ("back", true )
sleep(1)
rs.setOutput ("back", false )
sleep(1)
rs.setOutput ("back", true )
sleep(1)
rs.setOutput ("back", false )
else
if password == "2" then
print ("Closing Gate")
rs.setOutput ("top", true )
sleep(1)
rs.setOutput ("top", false )
sleep(1)
rs.seyOutput ("top", true )
sleep(1)
rs.setOutput ("top", false )
sleep(1)
rs.setOutput ("top", true )
sleep(1)
rs.setOutput ("top", false )
sleep(1)
rs.setOutput ("top", true )
sleep(1)
rs.setOutput ("top", false )
else
print ("Sorry, not a correct input.")
print ("Please try again!")
sleep(2)
os.shutdown()
end
1190 posts
Location
RHIT
Posted 17 August 2013 - 07:15 PM
Your problem is on the line where you have this:
else
if password == "2" then
You need to put the else and the if together into an elseif like so:
elseif password == "2" then
9 posts
Posted 17 August 2013 - 07:16 PM
Yup..fixed…Thanks!
1852 posts
Location
Sweden
Posted 17 August 2013 - 07:19 PM
Uhmm.. Bubba? Why didn't you notice that he posted the same topic before?
It was one the same page even :P/>
1190 posts
Location
RHIT
Posted 17 August 2013 - 07:32 PM
Uhmm.. Bubba? Why didn't you notice that he posted the same topic before?
It was one the same page even :P/>
It's hard to keep track of everything that goes on on these forums… I just split this from the New members topic and went directly to the thread.
@OP - in the future, please keep questions related to the same problem/same program in one topic. Not only does that allow us to keep the thread clean, it also lets us better help you. Thanks!
1522 posts
Location
The Netherlands
Posted 17 August 2013 - 07:49 PM
Your problem is on the line where you have this:
else
if password == "2" then
You need to put the else and the if together into an elseif like so:
elseif password == "2" then
First of all I want to say that this is the ultimate solution, but I just need to say this.
In a if-structe we have the elseif, Im glad to have that. But languages like Java use this structure:
if( statement ){
//execute thuse
} else /*if statement is false*/
if( another statement ) { // and like this you keep
}
so essentially you are just opening a new if block if the statement keeps evaluatibg false or null. That being said I can understand how the OP got confused.
So, by simply adding a few ends the code should run perfectly fine, however your method is the best for this.