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

else statesment issue

Started by signur, 06 January 2014 - 07:16 AM
signur #1
Posted 06 January 2014 - 08:16 AM
Hi,

As i am beginning to learn programation, and playing tekkit lite, i though of using computercraft to get familiarized with coding in a fun environment.
I'm basically a beginner.

In order to understand the code i went step by step, i'm now having fun with locks for doors.

Here is my attempt to make my lock, using iterative instead of recursive (or vice versa? i'm declaring function instead so that i can make my own library, it's iterative no?)


Spoiler(forget the dots, the preview don't show the indentation so i added dots. there not in the code.)

local password = "0000"
local exit = "exit"
local opentime = 5

function Lockgood()
….term.clear()
….term.setCursorPos(1,1)
….write("Access Granted")
….rs.setOutput("back", true)
….sleep(opentime)
….rs.setOutput("back", false)
….end

while true do
….term.clear()
….term.setCursorPos(1,1)
….write("Password:")
….term.setCursorPos(10,1)
….local input = read("*")

….if input == password then
……..Lockgood()
……..end
….if input == exit then
……..term.clear()
……..term.setCursorPos(1,1)
……..shell.run "shell"
……..end
….else
……..term.clear()
……..term.setCursorPos(1,1)
……..write("Access denied !")
……..end
end

it tells me that i need a end to close the while loop.

I tryed many things ut as far as i know i don't need an extra end…

What did i do wrong?

Thanks in advance. :)/>
Cranium #2
Posted 06 January 2014 - 10:17 AM
In your if/then loop, you need to specify different statements with elseif, rather than another if.
Example:

if something then
    --do something
elseif somethingElse then
    --do something else
else
    --do something else entirely
end
CometWolf #3
Posted 06 January 2014 - 10:59 AM
The probelm is the end in this section

....if input == exit then
........term.clear()
........term.setCursorPos(1,1)
........shell.run "shell"
........end
....else
There's no need for an end to close an if statement if it's followed by an else or elseif statement.
signur #4
Posted 06 January 2014 - 02:03 PM
i manage to fix it by myself while the post was pending approval.

Here is the new version of my lock :

Spoilerlocal password = "blastdoor"
local exit = "exit"
local opentime = 15

function Lockgood()
….term.setCursorPos(3,8)
….write("Access granted! Blastdoors will close in 15s")
….rs.setOutput("back", true)
….sleep(opentime)
….rs.setOutput("back", false)
….end

function GetOut()
……..term.setCursorPos(3,8)
……..write("Disconnecting")
……..sleep(0.2)
……..term.setCursorPos(17,8)
……..write(".")
……..sleep(0.2)
……..term.setCursorPos(18,8)
……..write(".")
……..sleep(0.2)
……..term.setCursorPos(19,8)
……..write(".")
……..sleep(0.2)
……..term.setCursorPos(20,8)
……..write(".")
……..sleep(0.2)
……..term.setCursorPos(21,8)
……..write(".")
……..sleep(0.2)
……..term.setCursorPos(22,8)
……..write(".")
……..sleep(0.2)
……..term.setCursorPos(23,8)
……..write(".")
……..sleep(0.2)
……..term.setCursorPos(24,8)
……..write(".")
……..sleep(0.2)
……..term.setCursorPos(25,8)
……..write(".")
……..sleep(0.2)
……..term.clear()
……..term.setCursorPos(1,1)
……..shell.run "shell"
……..end

function Lockwrong()
……..term.setCursorPos(3,8)
……..write("Access denied!")
……..sleep(2)
……..end

while true do
….term.clear()
….term.setCursorPos(1,1)
….write("O=================================================O")
….term.setCursorPos(1,2)
….write("I I")
….term.setCursorPos(1,3)
….write("I Kaer Tomkath Underground Bunker I")
….term.setCursorPos(1,4)
….write("I Blastdoor security controls I")
….term.setCursorPos(1,5)
….write("I Authorized personnel only I")
….term.setCursorPos(1,6)
….write("I I")
….term.setCursorPos(1,7)
….write("I Please enter password : I")
….term.setCursorPos(1,8)
….write("I I")
….term.setCursorPos(1,9)
….write("I I")
….term.setCursorPos(1,10)
….write("O=================================================O")
….term.setCursorPos(27,7)
….local input = read("*")

….if input == password then
……..Lockgood()
….elseif input == exit then
……..GetOut()
….else
……..Lockwrong()
….end
end

the issue resolved by itself. i understand that there was an end too much or missing.

I'll keep an eye on that.

little question before i end this topic (or am i allowed to follow the issue i encounter here?)

is there any way to get the console to close so that i don't have to hit escape ?
MR_nesquick #5
Posted 06 January 2014 - 03:14 PM
delete

local input = password
and change out
password = read("*")
with

local input = read("*")
and you should be good to go
remember to change the "read()" inside the while loop

edit:
you can also do
while not input == password do
makes it easier to read :)/>


EDIT EDIT: where did joe's question go?
Edited on 07 January 2014 - 12:11 PM
signur #6
Posted 06 January 2014 - 03:42 PM
delete

local input = password
and change out
password = read("*")
with

local input = read("*")
and you should be good to go
remember to change the "read()" inside the while loop


while not input == password do
makes it easier to read :)/>

EDIT EDIT: where did joe's question go?

could you give more explaination?

i'm not sure to follow.
Bomb Bloke #7
Posted 06 January 2014 - 04:32 PM
is there any way to get the console to close so that i don't have to hit escape ?
I believe not.