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

variable help

Started by emufossum13, 16 January 2013 - 02:20 PM
emufossum13 #1
Posted 16 January 2013 - 03:20 PM
Hello, I've been working on an automated atm using computercraft and redpower. And I'm kind of stuck.
Basically what I have it do is accept three types of currency. $1, $5, and $10 tokens. I have a for loop written that will repeat a amount of times based on what the user
inputs when it asks them "how much money do you want to put into the account. I write it as
for i = 1,a do
This way the function can be repeated as many times as the user wants to put in money. Within the function I have an if statement that deals with recognizing the different types of currency. My problem comes when I want the system to store the information in a variable. Say for the account, playerd, I set the variable that stores the balance as playerd. My problem comes when I try and get the system to keep that information. I want the variable to add to itself depending on what currency is inserted. I use

a = io.read()
y = 0
for i = 1,a do
playerd = y + 1
end
print(playerd)
os.sleep(2)

however, this doesn't work because all it does is reset the variable, so the variable playerd always equals 1.
Luanub #2
Posted 16 January 2013 - 03:24 PM
You don't need to use two variables, just use 1 and add it to itself. "y = y + 1"
remiX #3
Posted 16 January 2013 - 11:31 PM
When using read() and you want to use that variables as a number, you should convrrt it to a number with tonumbrr(read()) for loops
KaoS #4
Posted 17 January 2013 - 01:40 AM
why not just

a=read()
y=0
playerd=y+a
print(playerd)
sleep(2)
ChunLing #5
Posted 17 January 2013 - 08:22 AM
How is that different from:
print(read())
sleep(2)
I think that I need a better description of how this "currency insertion" process works. That's what's missing for me. As it is, the OP code (with some correction for apparent intention) could be replaced by:
playerd = playerd + read()
print(playerd)
sleep(2)
But this ignores the step where the "inserted currency" is evaluated and accepted/rejected/whatever.

I'm aware that this function may not exist yet, but having a placeholder could help with understanding the appropriate structure to use.
Edited on 17 January 2013 - 07:24 AM