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

Help With Indentation?

Started by Agoldfish, 08 November 2013 - 10:31 AM
Agoldfish #1
Posted 08 November 2013 - 11:31 AM
Hi, I am going to write a pretty long code, and I would like to know how you indent code. I know the basics, indent on blocks or something like that, but the way Lyqyd explained it here didn't make any sense to me, not because he explained it bad, just because I couldn't comprehend it. Anyway, could someone explain it to me in a way a beginner could understand? Thank you.

-Agoldfish
Lyqyd #2
Posted 08 November 2013 - 12:31 PM
A statement that starts a block and the statement that ends a block should be on the same level of indentation. The contents of that block should have one additional level of indentation:


do --# starts a block
  --# block contents
  print("block contents")
end --# ends a block

if true then --# starts a block
  print("looping")
  while true do --# starts a block
    print("in mah loop")
    break
  end --# ends a block
  repeat --# starts a block
    print("another block")
  until true --# ends a block
  print("whee")
  sleep(1)
  term.clear()
  term.setCursorPos(1, 1)
end --# ends a block

This is not a comprehensive example.
Agoldfish #3
Posted 08 November 2013 - 12:43 PM
So stuff that is in an else statement should be on one line, stuff in a then statement should be on another?
Agoldfish #4
Posted 08 November 2013 - 12:51 PM
Also, I would hate to make another topic. How is this incorrect? I get the 'then' expected error. I looked at the line above it, seeing if I accidentally used == instead of =, but I didn't. Here is the code.

--#The error is in line 3
print"Enter Username:"
username = read()
if username is == "Michael" then
print"Username Correct"
else
  print"Access Denied"
  sleep(1)
  os.reboot()
end
print"Enter Password:"
password = read()
if password is == "Gamma" then
   print"Access Granted"
else
	print"Access Denied"
	sleep(1)
	os.reboot()
end
Thanks for anyone who helps.
Edited on 08 November 2013 - 11:53 AM
Goof #5
Posted 08 November 2013 - 01:45 PM
Also, I would hate to make another topic. How is this incorrect? I get the 'then' expected error. I looked at the line above it, seeing if I accidentally used == instead of =, but I didn't. Here is the code.

--#The error is in line 3
print"Enter Username:"
username = read()
if username is == "Michael" then
print"Username Correct"
else
  print"Access Denied"
  sleep(1)
  os.reboot()
end
print"Enter Password:"
password = read()
if password is == "Gamma" then
   print"Access Granted"
else
	print"Access Denied"
	sleep(1)
	os.reboot()
end
Thanks for anyone who helps.

Well
Lua doesnt have the "is == " thingy… to check wether a string is true or anything else do this:

local script = "Hello! Dan200 is nice" -- A variable ALWAYS has a single equal sign ( thats what I know ) -- If statements has two equal signs..
-- Just a variable ^
-- then to the if statement:
if script == "Hello! Dan200 is nice" then -- to check it you just do the double equal signs instead of "is ==".
 --#Blah blah blah
elseif script ~= true then-- this could also be formatted by doing this:
 --#Blah blah blah
elseif script == false then -- or this:
 --#Blah blah blah
elseif script == nil then 
 --#Blah blah blah
else 
 --#Blah blah blah
end


I hope that helped :)/>
nomer888 #6
Posted 08 November 2013 - 01:47 PM

print"Enter Username:"
username = read()
if username == "Michael" then
  print"Username Correct"
else
  print"Access Denied"
  sleep(1)
  os.reboot()
end
print"Enter Password:"
password = read()
if password == "Gamma" then
   print"Access Granted"
else
  print"Access Denied"
  sleep(1)
  os.reboot()
end

You were using "if is then," is isn't valid. The correct syntax is "if then." I also added a left out indentation on line 4.
Agoldfish #7
Posted 08 November 2013 - 02:04 PM
Oh my goodness. I feel so stupid. I know that! I talk to myself line after line, and I wrote if input is like a total dummy. Thanks everyone!