im trying to make a program for a lua based computer in a game. Although when its run it acts weird
--Tablet
oldpullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw
while true do
term.clear()
term.setTextColor( colors.white )
term.setCursorPos(1, 1)
print("Please Enter Password:")
input = read("*")
incorrect = 0
while incorrect < 3 do
if input == "qwerty" then
print("Password Correct, Unlocking")
else
if incorrect < 3 then
incorrect = incorrect + 1
print("Password incorrect")
print(3 - incorrect, " tries remaining")
else
print(3 - incorrect, "tries remaining, locking phone for 1m")
local num = 0
while num < 60 do
if num < 60 then
term.clear()
term.setTextColor( colors.red )
term.setCursorPos(1, 1)
num = num + 1
print(60 - num, "s remaining")
sleep(1)
else
incorrect = 0
end
end
end
end
end
end
os.pullEvent = oldpullEvent
When it runs it starts with "Please enter password:" upon entering "qwerty" the password it wants, it loops "Password Correct, Unlocking" over and over infinitly. when I enter an incorrect password it doesnt run any of the code in the else statement and just returns back to the enter password screen. no error codes or crashes. Does anybody who know lua know if I wrote my while/if/elseif functions wrong or a work around.
Thanks!
When the correct password is entered the loop is not told to stop. After the correct password is entered a break
should be placed after print("Password Correct, Unlocking")
This is because the input
is outside the loop, a better approach would be something like this:
local incorrect = 0
while true do
term.clear()
term.setTextColor( colors.white )
term.setCursorPos(1, 1)
print("Please Enter Password:")
local input = read("*")
if input == "qwerty" then
print("Password Correct, Unlocking")
break
else
if incorrect < 2 then
incorrect = incorrect + 1
print("Password incorrect")
print(3 - incorrect, " tries remaining")
sleep(1) -- let them read the print.
else
print("out of attempts, locking phone for 1m")
for i = 10, 1, -1 do
term.clear()
term.setTextColor( colors.red )
term.setCursorPos(1, 1)
print(i, "s remaining")
sleep(1)
end
incorrect = 0
end
end
end
The above code will allow the user 3 attempts at the password, if all are used they are locked out for 60 seconds and given another 3 attempts. This repeats until the correct password is entered.
I have removed the inner while loop as it was not required. The incorrect
has been made local and moved outside of the while loop so it doesn't get reset every time the user enters a password.
The read("*")
has been moved inside the while loop so it prompts the user for a password every time rather than asking once and then looping infinitely.
The code has been tested and appears to work without any problems.
If any of the code doesn't make sense don't hesitate to ask.