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

Getting user input at a number, not string.

Started by Rankupgamers, 29 October 2012 - 04:19 PM
Rankupgamers #1
Posted 29 October 2012 - 05:19 PM
I am making a code to retrieve items from storage… I am trying to get a user to input a number (the amount of items they want) … It isn't working with input = read() because that returns a string. How would I change it to get back to a number and not a string?
remiX #2
Posted 29 October 2012 - 05:21 PM
tonumber(read())
Rankupgamers #3
Posted 29 October 2012 - 05:57 PM
Thanks… Now how would I get the program to restart after it finishes its rounds?
remiX #4
Posted 29 October 2012 - 06:14 PM
Use a while true do loop:


while true do
  term.clear() term.setCursorPos(1,1)
  print("Input a number")
  n = tonumber(read())
  if type(n) == "number" then
    print("You entered a number as a string and converted it into a number!")
  else
    print("You entered a number and it is still a string :/")
  end
  sleep(3) -- this is so you are able to view the message that is displayed
end