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

Atm help

Started by sflynn, 06 April 2014 - 12:41 AM
sflynn #1
Posted 06 April 2014 - 02:41 AM
Hey just an error that I cant fix nor can I find anything on the web to help me solve this.
ATM1:12:attempt to compare nil with number

term.clear()
local balance = 100
repeat
  term.setCursorPos(1,1)
  term.setTextColor(colors.red)
  term.write("====sflynn's User Friendly Banking balance = "..balance.."====")
  term.setTextColor(colors.blue)
  term.setCursorPos(1,2)
  term.write("how much would you like to withdraw: ")
  local withdraw = tonumber(read())
  while withdraw > balance and balance > 0 do
	balance = balance - withdraw --balance = balance [b]minus[/b] withdraw
  end
axel.codeFail() #2
Posted 06 April 2014 - 05:04 AM
I think I see the problem. When you run through it and withdraw x dollars, it works. but there is nothing to clear the amount that you entered before. So when you press enter the second time, the value of withdraw is nil.

EDIT :

I'd suggest adding the term.clear() into the loop.
Like so:

local balance = 100
repeat
  term.clear()
  term.setCursorPos(1,1)
  term.setTextColor(colors.red)
  term.write("====sflynn's User Friendly Banking balance = "..balance.."====")
  term.setTextColor(colors.blue)
  term.setCursorPos(1,2)
  term.write("how much would you like to withdraw: ")
  local withdraw = tonumber(read())
  while withdraw > balance and balance > 0 do
	    balance = balance - withdraw --balance = balance [b]minus[/b] withdraw
  end
until nil
Edited on 06 April 2014 - 03:06 AM
Bomb Bloke #3
Posted 06 April 2014 - 05:53 AM
If anything other then a number is entered (eg a $ symbol), tonumber(read()) will return nil.
sflynn #4
Posted 06 April 2014 - 04:45 PM
thanks!