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

[lua][help] Bank, where a little help.

Started by HelloThere, 10 August 2012 - 02:32 AM
HelloThere #1
Posted 10 August 2012 - 04:32 AM
I'm making a bank. well i already made the bank it takes things from seprate chests and gives you items. Theres just one problem i noticed:

in some places if you do not type the correct password or string it exits the program. i dont want that!!!

instead if you get something wrong i want it too:

>else
>print("Sorry that is an invalid answer!")
>sleep(1)
>os.reboot()

or something of that sort that may not reboot but just let you try again either way. i put this in many places already but there are still a few areas that doom it. Heres the overall code:



term.clear()
term.setCursorPos(1, 1)
print("Welcome to bank Prototype B")
sleep(3)
term.setCursorPos(1, 1)
print("Please Type your Artifical 5 digit Pin")
local input = read("*")
if input == ("OR-197") then
end
if input == ("Potato") then
print("welcome [USER] to your bank!")
print("Would you like to:")
print("Withdraw: Type W")
print("Deposit: Type D")
sleep(1)
local input = read()
if input == "D" then
term.clear()
term.setCursorPos(1, 1)
print("Deposits")
print("What would you like to deposit?")
print("Ingots: I")
print("Tools: T")
print("Food: F")
print("Miscellaneous: M")
local input = read()
if input == "I" then
rs.setBundledOutput("back", colours.lightGrey)
print("Type S to stop")
local input = read()
if input == "S" then
os.reboot()
else
print("Sorry that is not a valid answer!")
sleep(1)
os.reboot()
end
end
if input == "T" then
rs.setBundledOutput("back", colours.pink)
print("Type S to stop.")
local input = read()
if input == "S" then
os.reboot()
else
print("Sorry that is not a valid answer!")
sleep(1)
os.reboot()
end
end
if input == "F" then
rs.setBundledOutput("back", colours.grey)
print("Type S to stop")
local input = read()
if input == "S" then
os.reboot()
else
print("Sorry that is not a valid answer!")
os.reboot()
end
end
if input == "M" then
rs.setBundledOutput("back", colours.lime)
print("To stop press S")
local input = read()
if input == "S" then
os.reboot()
else
print("Sorry that is not a valid answer")
sleep(1)
end
end
end

if input == "W" then
term.clear()
term.setCursorPos(1, 1)
print("Withdrawls")
print("What would you like to withdraw?")
print("Ingots: I")
print("Tools: T")
print("Food: F")
print("Miscellaneous: M")
sleep(1)
local input = read()
if input == "I" then
rs.setBundledOutput("back", colours.white)
print("Type S to stop ore pumping.")
local input = read()
if input == "S" then
os.reboot()
else
print("Sorry that is not a vaild answer!")
sleep(1)
os.reboot()
end
end
if input == "T" then
rs.setBundledOutput("back", colours.red)
print("To stop press S")
local input = read()
if input == "S" then
os.reboot()
else
print("Sorry that is not a valid answer!")
sleep(1)
os.reboot()
end
end
if input == "F" then
rs.setBundledOutput("back", colours.black)
print("To stop press S")
local input = read()
if input == "S" then
sleep(1)
os.reboot()
else
print("Sorry that is not a vailid answer")
os.reboot()
end
end
if input == "M" then
rs.setBundledOutput("back", colours.green)
print("To stop press S")
local input = read()
if input == "S" then
os.reboot()
else
print("Sorry that is not a vialid answer")
sleep(1)
os.reboot()
end
end
end
end
[code]

its on pastebin here:
[url="http://pastebin.com/0tzir8eF"]http://pastebin.com/0tzir8eF[/url]

Can someone help me figure this out...? Thanks.

[Edit][Note] you'll notice above when you type in your password it has the code
>if input == "OR-197" then
> end

i wanted that to end the code
as an override.
KaoS #2
Posted 10 August 2012 - 01:44 PM
you should use elseif rather than making multiple if statements, for example in the first menu after login

you call so many is statements checking for various inputs, you check for 'D' and if it is not that then it restarts so already it will only ever check for 'D', what you should do is


if input=="d" then
-code
elseif input=="s" then
-code
elseif input=="t" then
-code
else
print("wrong answer")
sleep(1)
os.reboot()
end
HelloThere #3
Posted 10 August 2012 - 10:22 PM
Alright, i did that now i have just three questions.

1: Say i didn't want it to reboot every time someone mistype something but simply let them try again how would i do that?

2:Is there way to define a code to a variable? i know you can define numbers such as doing A=read() or B=230, but can a define a code like that? i tryed and simply typing C=term.clear() does not work.

3: Lastly, does this look more efficent? or do you have more tips of revising this code?

/code/

os.pullEvent = os.pullEventRaw
term.clear()
term.setCursorPos(1, 1)
print("Welcome to bank Prototype B")
sleep(1.5)
term.setCursorPos(1, 1)
print("Please Type your Artifical 5 digit Pin")
local input = read("*")
if input == ("OR-197") then
elseif input == ("Potato") then
print("welcome [USER] to your bank!")
print("Would you like to:")
print("Withdraw: Type W")
print("Deposit: Type D")
sleep(1)
local input = read()
if input == "D" then
term.clear()
term.setCursorPos(1, 1)
print("Deposits")
print("What would you like to deposit?")
print("Ingots: I")
print("Tools: T")
print("Food: F")
print("Miscellaneous: M")
local input = read()
if input == "I" then
rs.setBundledOutput("back", colours.lightGrey)
print("Type S to stop")
local input = read()
if input == "S" then
os.reboot()
else
print("Sorry that is not a valid answer!")
sleep(1)
os.reboot()
end
elseif input == "T" then
rs.setBundledOutput("back", colours.pink)
print("Type S to stop.")
local input = read()
if input == "S" then
os.reboot()
else
print("Sorry that is not a valid answer!")
sleep(1)
os.reboot()
end
elseif input == "F" then
rs.setBundledOutput("back", colours.grey)
print("Type S to stop")
local input = read()
if input == "S" then
os.reboot()
else
print("Sorry that is not a valid answer!")
os.reboot()
end
elseif input == "M" then
rs.setBundledOutput("back", colours.lime)
print("To stop press S")
local input = read()
if input == "S" then
os.reboot()
else
print("Sorry that is not a valid answer")
sleep(1)
os.reboot()
end
else
print("Sorry that is not a valid answer!")
sleep(1)
os.reboot()
end


elseif input == "W" then
term.clear()
term.setCursorPos(1, 1)
print("Withdrawls")
print("What would you like to withdraw?")
print("Ingots: I")
print("Tools: T")
print("Food: F")
print("Miscellaneous: M")
sleep(1)
local input = read()
if input == "I" then
rs.setBundledOutput("back", colours.white)
print("Type S to stop ore pumping.")
local input = read()
if input == "S" then
os.reboot()
else
print("Sorry that is not a vaild answer!")
sleep(1)
os.reboot()
end
elseif input == "T" then
rs.setBundledOutput("back", colours.red)
print("To stop press S")
local input = read()
if input == "S" then
os.reboot()
else
print("Sorry that is not a valid answer!")
sleep(1)
os.reboot()
end
elseif input == "F" then
rs.setBundledOutput("back", colours.black)
print("To stop press S")
local input = read()
if input == "S" then
sleep(1)
os.reboot()
else
print("Sorry that is not a vailid answer")
os.reboot()
end
elseif input == "M" then
rs.setBundledOutput("back", colours.green)
print("To stop press S")
local input = read()
if input == "S" then
os.reboot()
else
print("Sorry that is not a vialid answer")
sleep(1)
os.reboot()
end
else
print("Sorry that is not a valid answer!")
sleep(1)
os.reboot()
end
else
print("Sorry that is not a valid answer!")
sleep(1)
os.reboot()
end
else
print("sorry that is not a valid answer!")
sleep(1)
os.reboot()
end
/code/

does this look like a code that's impossible to cancel without means of other hardware or knowing the override code? (OR-197)?

Thanks for the help so far.

Pastebin:
http://pastebin.com/Skjs2mFw
Cranium #4
Posted 10 August 2012 - 10:31 PM
If you put a while true do loop in your code, you can ask for the password like you want. ex:

while true do --start loop
local pass = "password"
if pass == "password" then
--successful code here
else
print("Wrong password")
end
end
or, you can do this:

while true do
local pass = "password"
if pass ~= "password" then --tells it that if pass is anything except password then do this.
print("Wrong password")
else
break -- breaks the loop
end
end
-- successful code after your password is correct.
HelloThere #5
Posted 10 August 2012 - 10:34 PM
I did that then it constantly said "sorry that is not the password" until the program quit saying "too long without yielding"
Cranium #6
Posted 10 August 2012 - 10:51 PM
You can do the same thing.
Just repeat the one above with the break option. Give me a bit, and I'll come back with a cleaned up version of your code, and see if you like it.
Cranium #7
Posted 11 August 2012 - 02:12 AM
I had a little fun with your code. I did change it considerably, but kept the core fundamentals. Any changes I think might be neat to know have been noted:

--this is declaring your first options
DorW = {
"Deposit",
"Withdraw",
"Logout"
}
--this is declaring your withdrawal or deposit options
option = {
"Ingots",
"Tools",
"Food",
"Miscellaneous"
}
function opt(m) --this code used from the tutorials section of the CC forums.
n=1
l=#m
while true do
term.setCursorPos(1,4)
for i=1, l, 1 do
if i==n then print(">"..m[i].."<") else print(" ",m[i]," ") end
end
a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<=l then n=n+1 end
if b==28 then break end
end
end
term.clear() term.setCursorPos(1,1)
return n
end
function bank(c)
while true do
rs.setBundledOutput("back", colours.combine(c)) --we want to have it use colours.combine this time. (easier in the long run)
term.clear()
print("Press S to stop")
local event, p1 = os.pullEvent("key") --waits until the user preses a key
if p1 == 31 then --this is the code for "s"
rs.setBundledOutput("back", colours.subtract(c))
break --breaks the loop and asks for the PIN again
end
end
end
while true do
term.clear()
term.setCursorPos(1,1)
print("Welcome to Bank Prototype B")
sleep(1)
term.setCursorPos(1,2)
print("Please enter your Artificial 5 digit PIN")
local input = read("*")
if input == "OR-197" then --you do not need the () here
--or-197 stuff here
elseif input == "Potato" then
  while true do
  term.clear()
  term.setCursorPos(1,1)
  print("Welcome [USER] to your bank!")
  print("Would you like to:")
  local menu = opt(DorW)
   if menu == 1 then
   term.clear() term.setCursorPos(1,2)
   print("Deposit")
   print("What would you like to Deposit?")
   local w = opt(option)
    if w == 1 then
    bank(colours.lightGrey)
    elseif w == 2 then
    bank(colours.pink)
    elseif w == 3 then
    bank(colours.grey)
    elseif w == 4 then
    bank(colours.lime)
    end
   elseif menu == 2 then
   term.clear() term.setCursorPos(1,2)
   print("Withdraw")
   print("What would you like to Withdraw?")
   local w = opt(option)
    if w == 1 then
    bank(colours.white)
    elseif w == 2 then
    bank(colours.red)
    elseif w == 3 then
    bank(colours.black)
    elseif w == 4 then
    bank(colours.green)
    end
   elseif menu == 3 then break
   end
  end
else --this is if they enter the wrong PIN
print("That is an incorrect PIN. Try again.")
sleep(1)
end
end
HelloThere #8
Posted 11 August 2012 - 04:40 AM
I love the looks of the code, i'ts going into some things I've always wondered of but never got around to looking it up. Thanks so much! Unfortunatly I cannot try it until monday. But t thanks for this, i'ts saving me alot of trouble!

I love the looks of the code, i'ts going into some things I've always wondered of but never got around to looking it up. Thanks so much! Unfortunatly I cannot try it until monday. But t thanks for this, i'ts saving me alot of trouble!
HelloThere #9
Posted 13 August 2012 - 02:01 PM
Alright so i got the code all copied into a computer to try it and after i typed it all and ran it,
it did nothing. am i doing something wrong or is the code it self?
Cranium #10
Posted 13 August 2012 - 03:12 PM
Hmm… I'll have to test it out when I get off of work. When I wrote it, I tested it, and it worked at the time. I'll see what I can find out.
HelloThere #11
Posted 13 August 2012 - 11:14 PM
I actually got it too work except there's a small issue, when you press s to stop it dosen't stop it just goes back to the previous menu. which means it will not stop pumping. although it will stop if you start have it start something else such as getting items from a different chest. i can fix this easily by making it reboot on S but that's just not what i need it to do efficiency. any ideas?
Cranium #12
Posted 13 August 2012 - 11:31 PM
What you can do is instead of the line where it says rs.setBundledOutput("back", colours.subtract©), you can use rs.setBundledOutput("back", 0). "0" is just the code to stop all output.
HelloThere #13
Posted 13 August 2012 - 11:35 PM
Alright. Thank you!