0

when implementing for loop I get error 'for' limit must be a number and here is my code,and my input is number phone,can someone help me edit, let me complete. Thank you

local a = tonumber(nameInput.value); 
for  i = 1,tonumber(nameInput.value),1 do       

        line = fh:read()
        if line == nil then break end
        
        a = a +tonumber(nameInput.value);
1
  • 1
    It seems your nameInput.value does not contain a number. For example, it might be an empty string or nil Commented Mar 9, 2022 at 13:29

1 Answer 1

1

tonumber will return nil if the value passed to it can not be converted, a phone number like 123-456-7890 is not a valid input to tonumber it does not know what to do with the -s in the string.

Some documentation on tonumber: Lua 5.3 Manual: tonumber

to handle this you can use or 0 in lua to create a ternary of sorts.

local a = tonumber(nameInput.value) or 0; 
for  i = 1,tonumber(nameInput.value) or 0,1 do       

  line = fh:read()
  if line == nil then break end

  a = a +tonumber(nameInput.value);
end 

now when tonumber(nameInput.value) returns nil you will use the default value of 0. With this change you will no longer see the error but when nameInput.value is a non-numeric value you will also no enter your loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.