Jump to content




Advanced password program bug


  • You cannot reply to this topic
5 replies to this topic

#1 firelumet

  • Members
  • 5 posts

Posted 21 June 2014 - 05: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 by firelumet, 21 June 2014 - 05:42 PM.


#2 GamerNebulae

  • Members
  • 216 posts
  • LocationNetherlands

Posted 21 June 2014 - 08: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 by GamerNebulae, 21 June 2014 - 08:57 AM.


#3 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 21 June 2014 - 01: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

#4 firelumet

  • Members
  • 5 posts

Posted 21 June 2014 - 05: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

#5 firelumet

  • Members
  • 5 posts

Posted 21 June 2014 - 05: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


#6 firelumet

  • Members
  • 5 posts

Posted 21 June 2014 - 06:00 PM

Thank you again just fixed it and it works fine !





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users