math generator pls help

signs={"+","-"}
score=0
while true do
v1=math.random(1,10)
v2=math.random(1,10)
s=signs[math.random(1,#signs)]
answer=tostring(v1…s…v2)
answer=tonumber(answer)
problem=tostring(v1…s…v2)
print(problem…"=?")
guess=tonumber(io.read())
print("\n")
if guess==answer then
print(“correct! +100pts”)
score=score+100
else
print(“sorry, but the answer was “…answer…”. -50pts”)
score=score-50
end
end

mathgame.lua:9: attempt to concatenate global ‘answer’ (a nil value)

The end goal is to randomly generate math problems for the user to solve. I’m kind of a Lua newb and this is my 2nd application.

Please use the blue <> button and copy/paste your code from your editor into the popup window. It will make things easier to read.

You problem is the second line:

answer=tostring(v1…s…v2)
answer=tonumber(answer)

In the first line you make a string that will have “n + n”. In the second line you replace “answer” by trying to convert that back to a number. But since there is a + in the middle, it can’t be converted back, so answer becomes nil.

Now based on what you’re trying to do, you really don’t need the second line. You realistically don’t need to do the tostring() either because Lua will automatically make a string out of it when you concatenate the sign in.

Rob

Please use the blue <> button and copy/paste your code from your editor into the popup window. It will make things easier to read.

You problem is the second line:

answer=tostring(v1…s…v2)
answer=tonumber(answer)

In the first line you make a string that will have “n + n”. In the second line you replace “answer” by trying to convert that back to a number. But since there is a + in the middle, it can’t be converted back, so answer becomes nil.

Now based on what you’re trying to do, you really don’t need the second line. You realistically don’t need to do the tostring() either because Lua will automatically make a string out of it when you concatenate the sign in.

Rob