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

Need help with a while-loop of mine.

Started by blackbird820, 28 September 2012 - 09:17 AM
blackbird820 #1
Posted 28 September 2012 - 11:17 AM
Hello. I'm trying to create a while loop where the user must input a number into the computer, and check it for validation. My problem is, I can't figure out how to make it so that the input MUST be a number. Specifically, I want it to be a seven digit number. I have no problems when the input must be one of a few specific words, but I don't know how to set it so that the entry must be a number. Here's a segment of my code:


print("To Proceed, Please Validate Your Identity:")
print("")
while true do
write("Personal Identification Number: ")
id = read()
if id == ??? a 7 digit number ??? then
break
else
print("Invalid Entry")
print("Your ID# Must Be A 7 Digit Number")
sleep(2)
term.setCursorPos(1,5)
term.clearLine()
term.setCursorPos(1,4)
term.clearLine()
term.setCursorPos(1,3)
term.clearLine()
end
end


I don't care what number the user inputs, so long as it is seven digits long. I'd really appreciate any assistance :P/>/>
Doyle3694 #2
Posted 28 September 2012 - 11:31 AM
if tonumber(thestringyouread) == false then
print("The string you entered couldn't be recognized as a number, please try again.")
end
might work, will have to have a pro confirm this but I tihnk this should be right.
Fatal_Exception #3
Posted 28 September 2012 - 11:40 AM
You could check the length of the string for your number of digits, then tonumber() it and check that the return is not nil
GopherAtl #4
Posted 28 September 2012 - 02:38 PM
tonumber returns nil if it's not passes a number, not false. So..


if string.len(id)==7 and tonumber(id)~=nil then
  --code to run if it's good
else
  --code if it's bad
blackbird820 #5
Posted 28 September 2012 - 10:41 PM
Thanks guys! I managed to make something work. Appreciate the help!
KaoS #6
Posted 29 September 2012 - 08:21 AM
why tonumber through? just use if type(var)=='number' then