44 posts
Location
Australia
Posted 17 September 2012 - 03:23 AM
Hey ^_^
I am making a Password Door for my computer that Sends back Messages if the Password is wrong
i have been getting some error messages back from the computer
Error message:
>startup
bios:206: [string "startup":14: 'end' expected
(to close 'if' at line 8)
> _
Here's the code i am trying to fix
pez = 5464
while true do
term.clear()
term.setCursorPos(1, 1)
print("LithiumOS")
print("Please Enter password")
guess = io.read()
if guess == pez then
print("correct")
redstone.setOutput("right", true)
Sleep(5)
redstone.setOutput("right", false)
break
if guess < pez then
Print("Access Denied")
print("10 sec delay")
sleep(10)
elseif guess > pez then
print("Access Denied")
print("10 sec delay")
sleep(10)
end
end
end
i have no idea how to fix it and i have been trying for at least a week
Any idea how to fix?
474 posts
Posted 17 September 2012 - 03:59 AM
Shiny new fixed code:
pez = 5464
while true do
term.clear()
term.setCursorPos(1, 1)
print("LithiumOS")
print("Please Enter password")
guess = read()
if guess == pez then
print("correct")
redstone.setOutput("right", true)
sleep(5)
redstone.setOutput("right", false)
error()
if guess < pez then
print("Access Denied")
print("10 sec delay")
sleep(10)
elseif guess > pez then
print("Access Denied")
print("10 sec delay")
sleep(10)
end
end
end
Apparently
break
was the culprit, changing that to
error()
fixed it.
Though you may wanna change the logic as Maome said below me.
12 posts
Posted 17 September 2012 - 04:09 AM
break is used to exit loops so it's trying to exit the while true loop started at line 2, this should be removed
Your if statements are nested so they will only be checked if the first one is true
[indent=1]That is "if pez > 5464" is only checked if pez==5464 which will never happen.[/indent]
You probably want to change the whole if logic to something like:
if guess == pez then
--code for right password
else
--code for wrong password
--this section will run any time "guess == pez" is not true
--no need for checking both higher or lower
end
And lastly io.read() is going to give you back a string instead of a number. You could convert this to a number but it would be easier to set pez to a string. Instead of pez = 1111 pez = "1111" will put it in the same type as io.read() returns.
44 posts
Location
Australia
Posted 17 September 2012 - 04:36 AM
Shiny new fixed code:
Code Below Quote
Apparently
break
was the culprit, changing that to
error()
fixed it.
Though you may wanna change the logic as Maome said below me.
Using this code
The Script Runs now
But!
But When Correct password is entered It doesn't loop
Also When a Incorrect Password is entered it loops but it doesn't Give an message
CODE:
pez = "5464"
while true do
term.clear()
term.setCursorPos(1, 1)
print("LithiumOS")
print("Please Enter password")
guess = read()
if guess == pez then
print("correct")
redstone.setOutput("right", true)
sleep(5)
redstone.setOutput("right", false)
error()
if guess < pez then
print("Access Denied")
print("10 sec delay")
sleep(10)
elseif guess > pez then
print("Access Denied")
print("10 sec delay")
sleep(10)
end
end
end
12 posts
Posted 17 September 2012 - 04:43 AM
It doesn't error out with your changes but the logic is still faulty. You can either try to make the modifications mentioned in my earlier post if you're a DIY type or you might prefer a guide with pre-written code like
this one which does a pretty good job explaining everything going on if you're interested.
44 posts
Location
Australia
Posted 17 September 2012 - 12:43 PM
Fixed the Code
Made some Changes to the Way it Reads the Numbers
Instead of it Reading a Variable i made it so the Code is written in the Line
I also put " around the Value so it can Use it in maths
Here's the Working code for whoever wants to Use it
while true do
term.clear()
term.setCursorPos(1, 1)
print("LithiumOS")
print("Please Enter password")
guess = read()
if guess == 5464 then
print("correct")
redstone.setOutput("right", true)
sleep(5)
redstone.setOutput("right", false)
else
print("Access Denied")
print("10 sec delay")
sleep(10)
end
end