It looks like you're new here. If you want to get involved, click one of these buttons!

I'm working on a program that solves math problems using the four randomly generated numbers AND randomly generated math operators (x, /, +, -). The answer needs to be a calculated number, but so far the answer is an exact copy of the original math problem. For instance, if the math problem is: "10+5/8x2" the answer should be: "11.25" rather than: "10+5/8x2". I can understand why it's doing what it is doing, I just need it to insert the proper math operator in place of the text version. That's the sticking point.

The intent of this program is to drill an elementary-school-aged nephew in basic math concepts by presenting a math problem to solve then displaying the answer once complete. This is a stripped-down version for simplicity.

Any advice, of course, is much appreciated. Thanks.

```
-- OrderOfOperation
supportedOrientations(LANDSCAPE_ANY)
displayMode(STANDARD)
function setup()
print("OrderOfOperation:\nSolves a simple math problem while taking Order-Of-Operation rules into account.\n\nTouch screen for next problem.")
-- Randomly generate four numbers and three operations: multiply, divide, add and subtract
opTypeTbl = {" x "," / "," + "," - "}
first = math.random(1,10)
op1 = opTypeTbl[math.random(1,4)]
second = math.random(1,10)
op2 = opTypeTbl[math.random(1,4)]
third = math.random(1,10)
op3 = opTypeTbl[math.random(1,4)]
fourth = math.random(1,10)
end
function draw()
background(40, 40, 50)
Lab(first,op1,second,op2,third,op3,fourth)
end
function Lab(first,op1,second,op2,third,op3,fourth)
-- Display Problem and Answer given four random numbers and three random operators.
local first = first
local op1 = op1
local second = second
local op2 = op2
local third = third
local op3 = op3
local fourth = fourth
fill(255, 255, 255, 255)
fontSize(40)
local p = "Problem: "..first..op1..second..op2..third..op3..fourth
text(p, WIDTH/2,HEIGHT/2+100) -- Example problem: "10 + 5 / 8 x 2"
local a = "Answer: "..first..op1..second..op2..third..op3..fourth
-- Answer: 11.25
text(a,WIDTH/2,HEIGHT/2-100)
end
function touched(t)
-- Calls next problem:
if t.state == ENDED then
restart()
end
end
```

## Comments

@Scotty Hopefully this is what you're looking for:

Thank you, @Steppers .

That is exactly what I am looking for. However, there is some mystery in the line that has the "load" statement. I'll have to study up on that and your use of gsub.

If you rerun the modified version you'll see what my goal was. This will be blended into a more involved program.

Thanks again for the simple solution and the quick response.

The load just takes the string provided, compiles it, and runs it as lua code. The "return" in the one provided makes the expression's value come back as the result of the call to load.

@Steppers and @RonJeffries , I learn something new everyday. Thanks you.