Search code examples
lua

Assistance related to Lua programming where Basic Calculator need to build using 3 operators addition, subtraction and multiple


We had given below task

Create three function using Lua which can perform addition, subtraction and multiplication/

The program should be

Accept any number. Check the operator + - *. If not the print "Invalid operand" and exit. Accept two operands. if any operand not number print "Invalid operand" and exit. use - tonumber() Perform the operation and print result.

We had written code as below which not working as expected , not sure where we went wrong, please guide us

local operators = {
    ["+"] = function(x,y) return x+y end,
    ["-"] = function(x,y) return x-y end,
    ["*"] = function(x,y) return x*y end
}

local function default()
    print "Invalid operand"
end

local num1 = io.read()
local num2 = io.read()
local operator = io.read()

local func (
 if operator == "+" then
   local add = tonumber(num1) + tonumber(num2)   
 end 
 if operator == "-" then
   local subtract = tonumber(num1) - tonumber(num2)
 end
 if operator == "*" then
   local multiply = tonumber(num1) * tonumber(num2)
 end
)
or default
print(func(num1,num2))
io.read()

Correct code is

local operators = {
  ["+"] = function (x, y) return x + y end,
  ["-"] = function (x, y) return x - y end,
  ["*"] = function (x, y) return x * y end,
}

local operator = operators[io.read()]
local num1 = tonumber(io.read())
local num2 = tonumber(io.read())

if num1 and num2 then
  if operator then
    print(operator(num1, num2))
  else
    print("Invalid operator")
  end
else
  print("Invalid operand")
end

Solution

  • Your code has syntax errors and doesn't make too much sense.

    local func (
     if operator == "+" then
       local add = tonumber(num1) + tonumber(num2)   
     end 
     if operator == "-" then
       local subtract = tonumber(num1) - tonumber(num2)
     end
     if operator == "*" then
       local multiply = tonumber(num1) * tonumber(num2)
     end
    )
    or default
    

    To define a local function you can write

    local myFunction = function() end
    

    or

    local function myFunction() end
    

    But not

    local func()
    

    As you define the function it can never be nil. So short-circuiting a function definition like

    local default = function() end
    local myFunction = function() end or default
    

    doesn't make sense.

    You should add instructions so the user knows what to enter befor you call io.read().

    This part of your code is not used at all:

    local operators = {
        ["+"] = function(x,y) return x+y end,
        ["-"] = function(x,y) return x-y end,
        ["*"] = function(x,y) return x*y end
    }
    

    Also your function func does nothing but creating local variables which are not used.

    The function does not return a value so printing it's return value like print(func(num1, num2)) won't print a result as intended.

    You also fail to check if the user actually entered valid characters like numbers or operators. This will cause Lua errors if the user inputs something else.