Always returning wrong simple math

I have a very simple Lua script that asks a math question

math.randomseed(os.time())
        print ("Let play Math")
        a = math.random(1,10)
        b = math.random(1,10)
        io.write("What is " .. a .. " + " .. b .. "?")
        answer = io.read()
        correct = (a + b)
            if (answer == correct) then
                print ("Correct")
            else
                print ("Wrong")
                print (correct) --For debug
            end

      

For some reason I always get "wrong" even when answered correctly. I also print out the correct answer, just to make sure the program handles the math correctly. Where is my mistake?

+3


source to share


1 answer


answer

contains a string and is therefore never equal to a number.

Just add

    answer = tonumber(answer)

      



after

    answer = io.read()

      

+5


source







All Articles