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

Advanced password program bug

Started by firelumet, 21 June 2014 - 04:29 AM
firelumet #1
Posted 21 June 2014 - 06:29 AM
I made an advanced password program with tries and auto-lock. But I have a question : Each time it I "voluntary" make a mistake, it has a bug while counting and I can't set it to 0 every time :(/>. It says word by word : password:21: attempt to call arithmetic __add on nil and number
  • local password = "Password" – Password
  • local side = "left" – Side to send the redstone signal
  • local time = 5 – Activation time for the signal
  • local totalTry = 5 – Total tries before self-locking
  • while true do
  • term.clear()
  • local try = try
  • print("Password : ")
  • local input = read("*")
  • if input == password then
  • term.clear()
  • print("Password correct !")
  • rs.setOutput(side, true)
  • local try = 0
  • os.sleep(time)
  • rs.setOutput(side, false)
  • else
  • term.clear()
  • print("Wrong password !")
  • local try = try+1
  • if try == totalTry then
  • print("Computer locked for 60 seconds !")
  • os.sleep(60)
  • else
  • term.write("Essaie restante : ")
  • term.write(totalTry-try) – The error says to be here !
  • term.write(" !")
  • end
  • os.sleep(2)
  • end
  • end
Edited on 21 June 2014 - 04:42 PM
GamerNebulae #2
Posted 21 June 2014 - 09:57 AM
First things first, if you paste code on the forums, please use indentations and place the code in between the CODE /CODE brackets. You don't have to declare a local variable twice. You declared the variable try to be local in the if-statement. That's why it doesn't exist after the if-statement. Remove the local:

term.clear()
print("Wrong password!")
try = try + 1
Edited on 21 June 2014 - 07:57 AM
TheOddByte #3
Posted 21 June 2014 - 02:00 PM
Just so you know a little more about local variables

local str = "bar"
if str then
    local str = "foo"
    print( str )
end
print( str )
The code above would output

foo
bar
because when first declaring the local variable str to bar it's local to the program, while the second time it's local it's only used in the if statement
firelumet #4
Posted 21 June 2014 - 06:38 PM
Thank you and I didn't know, when I posted it, it auto-formed it :3 sorry I will fix it for the next time
firelumet #5
Posted 21 June 2014 - 06:53 PM
I will repost the code just to be clear (and I updated it a bit :3) :

local password = "Lancelot" -- Mot de Passe
local side = "left" -- Coter åA0 envoyer le signal
local time = 5 -- Temps d'activation du signal
local totalTry = 5 -- Essaie rater avant de se lock.
-- Don't touch to anything here unless you are crazy !--

while true do
local try = try
term.clear()
term.setCursorPos(1,1)
write("Mot de passe : ")
local input = read("*")
if input == password then
  term.clear()
  term.setCursorPos(1,1)
  print("Mot de passe correcte !")
  rs.setOutput(side, true)
  try = 0
  os.sleep(time)
  rs.setOutput(side, false)
else
  term.clear()
  term.setCursorPos(1,1)
  print("Mot de passe invalide !")
  try = try+1
   if try >= totalTry then
    print("Ordinateur bloquer pendant 60 secondes !")
    os.sleep(60)
    try = 0
   else
    term.write("Essaie restante : ")
    term.write(totalTry-try)
    term.write(" !")
   end
  os.sleep(2)
end
end
firelumet #6
Posted 21 June 2014 - 07:00 PM
Thank you again just fixed it and it works fine !