3

I've been looking around and I have not been able to find anything that has worked for me. I'm starting to learn more Lua and to start off I'm making a simple calculator. I was able to get each individual operation onto separate programs, but when I try to combine them I just can't get it to work. My script as it is now is

require "io"
require "operations.lua"

do
print ("Please enter the first number in your problem.")
x = io.read()
print ("Please enter the second number in your problem.")
y = io.read()
print ("Please choose the operation you wish to perform.")
print ("Use 1 for addition, 2 for subtraction, 3 for multiplication, and 4 for division.")
op = io.read()
op = 1 then
    function addition
op = 2 then
    function subtraction
op = 3 then
    function multiplication
op = 4 then
    function division
print (answer)
io.read()
end

and my operations.lua script is

function addition
    return answer = x+y
end

function subtraction
    return answer = x-y
end

function multiplication
    return answer = x*y
end

function division
    return answer = x/y
end

I've tried using

if op = 1 then
      answer = x+y
      print(answer)
if op = 2 then
      answer = x-y
      print(answer)

and I did that completing each operation. But it doesn't work. I can't even get the error code that it's returning because it closes so fast. What should I do?

3 Answers 3

1

In your example, make these changes: You require operations.lua without the extension. Include parameters in your operations function definitions. Return the operation expression directly versus returning a statement like answer = x+y.

All together:

Code for operations.lua

function addition(x,y)
    return x + y
end

--more functions go here...

function division(x,y)
    return x / y
end

Code for your hosting Lua script:

require "operations"

result = addition(5,7)
print(result)

result = division(9,3)
print(result)

Once you get that working, try re-adding your io logic.

Keep in mind that as it's coded, your functions will be defined globally. To avoid polluting the global table, consider defining operations.lua as a module. Take a look at the lua-users.org Modules Tutorial.

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

1 Comment

Thank you, I'll try working on it when I get home. It's hard to work without access to SciTE because I can't get the error to show for me to solve it.
1

The right if-then-else syntax:

if op==1 then
   answer = a+b
elseif op==2 then
   answer = a*b
end
print(answer)

After: please check the correct function-declaration syntax.

After: return answer=x+y is incorrect. If you want set answer's value, set without return. If you want return the sum, please use return x+y.

And I think you should check Programming in Lua.

1 Comment

Thank you, I've been having trouble with the if-then-else syntax. I'm still a beginner in Lua, I've only written two scripts other than this calculator.
0

First of all, learn to use the command line so you can see the errors (on Windows that would be cmd.exe).

Second, change the second line to require("operations"). The way you did it the interpreter expects a directory operations with an underlying script lua.lua.

1 Comment

I'd use the command line if it wasn't blocked. Right now I'm working from my school computer and cmd.exe is a blocked application. I had it originally as require "operations" but I changed it to operations.lua since operations wasn't working. I have SciTE at my house and that's what I was using to view errors yesterday when I worked on the main part of the script. I'll work on it at home and see if I can figure it out. Thank you for helping.

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.