c = a m b
it says '=' exepted but i tried to add = everywhere!a = a number
b = another number
m = + / - / * / :
What is wrong?
I'm attempting to do a simple caculator
c = a m b
it says '=' exepted but i tried to add = everywhere!
local n1 = 5
local n2 = 10
local calculation = n1 + n2
-- If you print calculation 15 will come out of it
I think you can't save math operators in variables :)/>/>
local a = 2
--# we cannot save mathematical operators into a variable, we must save them as a string
local m = '+'
local b = 5
--# we need 3 things, the two numbers, and the mathematical operator in string form
local function calculate1(num1, num2, op)
--# now we check which operator it is and return the result based off that operator
if op == '+' then
return num1 + num2
elseif op == '-' then
return num1 - num2
elseif op == '/' then
return num1 / num2
elseif op == '*' then
return num1 * num2
end
--# no valid operator supplied
error('Invalid operator: '..tostring(op), 0)
end
--# we need 3 things, the two numbers, and the mathematical operator in string form
local function calculate2(num1, num2, op)
--# we use loadstring to put our values into a statement
local func = loadstring( 'return '..num1..' '..op..' '..num2 )
--# run the loadstring function
return func()
--# note we can do the above 2 lines in one line, I split it out to make it more apparent whats happening, this is how you would do it in one line
--# return loadstring( 'return '..num1..' '..op..' '..num2 )()
end
print(calculate1(a, b, m))
print(calculate2(a, b, m))
Thanks! is there any way of putting like 10-2 into a single input (10 would ne num1 - will be op and 2 will be num2)?ok so this is 2 of the ways to get what you want done.
just our numbers to test withlocal a = 2 --# we cannot save mathematical operators into a variable, we must save them as a string local m = '+' local b = 5
method 1--# we need 3 things, the two numbers, and the mathematical operator in string form local function calculate1(num1, num2, op) --# now we check which operator it is and return the result based off that operator if op == '+' then return num1 + num2 elseif op == '-' then return num1 - num2 elseif op == '/' then return num1 / num2 elseif op == '*' then return num1 * num2 end --# no valid operator supplied error('Invalid operator: '..tostring(op), 0) end
method 2--# we need 3 things, the two numbers, and the mathematical operator in string form local function calculate2(num1, num2, op) --# we use loadstring to put our values into a statement local func = loadstring( 'return '..num1..' '..op..' '..num2 ) --# run the loadstring function return func() --# note we can do the above 2 lines in one line, I split it out to make it more apparent whats happening, this is how you would do it in one line --# return loadstring( 'return '..num1..' '..op..' '..num2 )() end
test the functionsprint(calculate1(a, b, m)) print(calculate2(a, b, m))
Not quite sure what you mean. do you mean like the loadstring method where its all put into one string?Thanks! is there any way of putting like 10-2 into a single input (10 would ne num1 - will be op and 2 will be num2)?
I mean if i input like 3+4 it can count itNot quite sure what you mean. do you mean like the loadstring method where its all put into one string?Thanks! is there any way of putting like 10-2 into a single input (10 would ne num1 - will be op and 2 will be num2)?
Count or calculate? i.e. would the input say 3 (number of parts) or 7 (the sum of 3 and 4)I mean if i input like 3+4 it can count it
Whatever…Count or calculate?I mean if i input like 3+4 it can count it
if i input 3+4 it will say: 7i.e. would the input say 3 (number of parts) or 7 (the sum of 3 and 4)
If you mean calculate then BOTH of the methods that I provided will calculate the result of 3+4 and return the value.
In that case you would just use loadstring instead of passing two values and an operator to the calculate function.if i input 3+4 it will say: 7
Like So:
>program
What would you like to caculate ( or count i don't know): 3+4
It is 7!
>_