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

Help with password program

Started by MadSientist277, 14 April 2013 - 07:08 AM
MadSientist277 #1
Posted 14 April 2013 - 09:08 AM
local pass = ""
term.write ("Please enter the password: ")
read "pass"
if "pass" ~= "ppp" then
print ("Correct password.")
term.write ("program starting")
sleep (1)
term.write (".")
sleep (1)
term.write (".")
sleep (1)
term.write (".")
else
print ("Incorrect password.")
shell.run "funct"
end

I know that I will have ppl shouting at me for my 'unproductive, wrong' code but its my first program. when ever I run it it works fine but when I try type the password, whatever key I type shows up as a 'p' but even when I set the password to say, 'ppp' it still runs the incorrect password line. can you please tell me if its a bug or my code is wrong. can u also tell me how to end a loop in the main computer screen without breaking the computer! thnx in advance PS: sorry if I posted it in the wrong place
Lyqyd #2
Posted 14 April 2013 - 05:43 PM
You should have posted your question in the sticky topic meant specifically for new members to ask questions in so that they can be given their own topic. It's even called "New Members Needing to Ask Questions".
Smiley43210 #3
Posted 14 April 2013 - 06:08 PM
Ok.

1. Don't surround variables with quotes
2. This:
if pass == "ppp" then
print ("Correct password.")
This literally translates to 'everything not equal to "ppp" is ok. Let's change that…
3. Everything you typed when you did read() turned into a 'p' because you did read "ppp". That's essentially doing read("ppp"). What that does is VISUALLY replace everything you type with the first letter in that string (so you can't see what you typed). It's only visual; it still returns what you actually typed.
4. Doing read "pass" won't set the return value of read() to pass. You have to do pass = read()
5. I hope you have a file named "funct", since shell.run("funct") will try to run the file with that name.

Also, if you want, use read("*"). That way, people can't see the password.


Corrected:

term.write ("Please enter the password: ")
local pass = read "*"
if pass == "ppp" then
print ("Correct password.")
term.write ("program starting")
sleep (1)
term.write (".")
sleep (1)
term.write (".")
sleep (1)
term.write (".")
else
print ("Incorrect password.")
shell.run "funct"
end
Smiley43210 #4
Posted 14 April 2013 - 06:12 PM
can u also tell me how to end a loop in the main computer screen without breaking the computer!
Well, depends. To get out of a while/for loop, use break. If you're talking about exiting a program, try holding down Ctrl + T for a few seconds.

Edit: Whaaat…oops! Sorry for the double post, I thought I clicked 'edit'.
Edit 2: Well, looks like I DID click edit, but I typed in the wrong box -_-/>.