0

What am I doing wrong with this Lua code? I am trying to generate two random numbers and ask what they are multiplied together. The first part is good, but no matter what I type as the answer, it always thinks that it is incorrect. Please tell me what I am doing wrong and how to fix it. Remember this is Lua.

math.randomseed(os.time())
local a=math.random(10)
local b=math.random(10)


local answer
repeat
io.write("What is ",a,"*",b,"?")    
io.flush()  
answer=io.read()
if answer==a*b then
print("Correct!")
else
print("Try Again")
end
until
answer==a*b

1 Answer 1

4

io.read() returns a string, and you're comparing it with a number.

You either need to say answer=tonumber(io.read()), or you need to say io.read("*n").

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

1 Comment

@user2059810 welcome to stackoverflow! consider accepting Kevin's answer (by clicking the check mark) to give him some credit and show future visitors that (and how) your problem has been solved

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.