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

[LUA] [Error]

Started by dragonherald, 07 March 2013 - 12:38 PM
dragonherald #1
Posted 07 March 2013 - 01:38 PM
Hey, I'm relatively new to ComputerCraft and programming in general. I was following a couple of tutorials trying to figure out how to set up a pass word lock on the computer. I had it to the point of working in reverse. It would re run the start up program if I inputted the correct password and do the opposite if it was the incorrect password. Then I started trying to fix that problem and eventually ran in to error
bios:338: [string "startup"]:6: 'then' expected
I rose the white flag after about five minutes of tinkering with line 6. Here's my code, all help is appreciated in advance.


local password = "password"
while true do
term.clear ()
term.setCursorpos (1, 1)
print ("Please enter password!")
  if password = true then
  term.clear ()
  term.setCursorpos (1, 1)
  sleep(2)
else
  print ("dumbass...")
  sleep(2)
end
end
SuicidalSTDz #2
Posted 07 March 2013 - 01:47 PM
Hey, I'm relatively new to ComputerCraft and programming in general. I was following a couple of tutorials trying to figure out how to set up a pass word lock on the computer. I had it to the point of working in reverse. It would re run the start up program if I inputted the correct password and do the opposite if it was the incorrect password. Then I started trying to fix that problem and eventually ran in to error
bios:338: [string "startup"]:6: 'then' expected
I rose the white flag after about five minutes of tinkering with line 6. Here's my code, all help is appreciated in advance.


local password = "password"
while true do
term.clear ()
term.setCursorpos (1, 1)
print ("Please enter password!")
  if password = true then
  term.clear ()
  term.setCursorpos (1, 1)
  sleep(2)
else
  print ("dumbass...")
  sleep(2)
end
end
In your <if password = true then> statement you must have two ='s since you are comparing two things. Don't confuse this with variables, variables are defined with one "=", not two.

if password == true then

variable = "this is a variable"
dragonherald #3
Posted 07 March 2013 - 02:10 PM
Hey, I'm relatively new to ComputerCraft and programming in general. I was following a couple of tutorials trying to figure out how to set up a pass word lock on the computer. I had it to the point of working in reverse. It would re run the start up program if I inputted the correct password and do the opposite if it was the incorrect password. Then I started trying to fix that problem and eventually ran in to error
bios:338: [string "startup"]:6: 'then' expected
I rose the white flag after about five minutes of tinkering with line 6. Here's my code, all help is appreciated in advance.


local password = "password"
while true do
term.clear ()
term.setCursorpos (1, 1)
print ("Please enter password!")
  if password = true then
  term.clear ()
  term.setCursorpos (1, 1)
  sleep(2)
else
  print ("dumbass...")
  sleep(2)
end
end
In your <if password = true then> statement you must have two ='s since you are comparing two things. Don't confuse this with variables, variables are defined with one "=", not two.

if password == true then

variable = "this is a variable"

Alright, switched some stuff around after receiving a few more errors. Then I eventually ended up with it working almost completely correctly it asks for the password I insert the password but it doesn't break the loop. I tested the else and it works as it should the current code is

local password = "password"
while true do
term.clear ()
term.setCursorpos (1, 1)
print ("Please enter password!")
  local input = read ("*")
  if input == password then
  term.clear ()
  term.setCursorpos (1, 1)
  sleep(2)
else
  print ("dumbass...")
  sleep(2)
end
end

Obviously I'm missing the command that breaks the loop and I've searched through some forums and wikis and haven't been able to understand what fixes it. It seems that most stuff on the internet is made for those with a basic understanding. I should probably buy the LUA guide book or torrent it.
SuicidalSTDz #4
Posted 07 March 2013 - 03:09 PM
local password = "password"
repeat
 term.clear ()
 term.setCursorpos(1, 1)
 print ("Please enter password!")
 local input = read ("*")
  if input == password then
   term.clear ()
   term.setCursorpos(1, 1)
   sleep(2)
 else
   print ("dumbass...")
   sleep(2)
 end
until input == password

I am a fan of using "repeat until" loops in this occasion. You also can find a FREE Lua Reference manual here (Which I advise everyone to read)
EDIT: Your "if" statement block was indented almost correctly, so I fixed it up

EDIT: To break loops, insert <break> wherever the code is to be ended (Break will break the current loop at that line, meaning everything after break will not be read!)
dragonherald #5
Posted 07 March 2013 - 03:17 PM
local password = "password"
repeat
term.clear ()
term.setCursorpos(1, 1)
print ("Please enter password!")
local input = read ("*")
if input == password then
   term.clear ()
   term.setCursorpos(1, 1)
   sleep(2)
else
   print ("dumbass...")
   sleep(2)
end
until input == password

I am a fan of using "repeat until" loops in this occasion. You also can find a FREE Lua Reference manual here (Which I advise everyone to read)
EDIT: Your "if" statement block was indented almost correctly, so I fixed it up

Okay, yeah. I went back through everything and rewrote it using a "repeat until" and it works perfectly now. Thanks for the assistance.
SuicidalSTDz #6
Posted 07 March 2013 - 03:18 PM
Okay, yeah. I went back through everything and rewrote it using a "repeat until" and it works perfectly now. Thanks for the assistance.
Glad I could help :)/>
3ydney #7
Posted 07 March 2013 - 09:57 PM
Well I can't believe that NO ONE saw:

term.setCursorpos(1, 1)



What is wrong with that LINE OF CODE?

term.setCursorpos() should be term.setCursorPos()


Should have gonna to Spec Savers < Only Aussies will get this.
SuicidalSTDz #8
Posted 07 March 2013 - 10:06 PM
Well I can't believe that NO ONE saw:

term.setCursorpos(1, 1)



What is wrong with that LINE OF CODE?

term.setCursorpos() should be term.setCursorPos()


Should have gonna to Spec Savers < Only Aussies will get this.
It only matters that YOU saw it :P/>