print("Math?")
mathInput = read()
print(mathInput*2)
For example, if I input:
(5+5)*5
I should get:
100
print("Math?")
mathInput = read()
print(mathInput*2)
(5+5)*5
I should get:
100
print("Number: ")
local input = tonumber( read() )
print( input * 2 )
This also presents a problem of anything that is not a number the input variable is nil, so to combat this you should make a loop that will only continue when the input is a number, and tell the user when they didn't input a number.this is because the input comes in as a string. What you need to do is convert the string into a number, before trying to calculate the result, with tonumber like soThis also presents a problem of anything that is not a number the input variable is nil, so to combat this you should make a loop that will only continue when the input is a number, and tell the user when they didn't input a number.print("Number: ") local input = tonumber( read() ) print( input * 2 )
local input = read() -- Get input.
local eval, err = loadstring("return " .. input) -- Compile the string.
if err then print("Could not evaluate. " .. err) end -- Check for a parse error.
print(eval()) -- Print the result.
Hey guys! On the lua console you can really easily input a math problem and get the simplified answer. However, if I ask for userInput and read it, I can't perform any arithmetic on it. Here's an example code:print("Math?") mathInput = read() print(mathInput*2)
For example, if I input:I should get:(5+5)*5
100
Sorry about not providing enough info. I never know what to give and what not to give. Unfortunately this won't really solve my problem, as Yevano explained..this is because the input comes in as a string. What you need to do is convert the string into a number, before trying to calculate the result, with tonumber like soThis also presents a problem of anything that is not a number the input variable is nil, so to combat this you should make a loop that will only continue when the input is a number, and tell the user when they didn't input a number.print("Number: ") local input = tonumber( read() ) print( input * 2 )
For future reference, when asking for help in Ask a Pro, we require more information than you provided here in this one, especially if you want to get help quicker! Please have a read of this thread especially the last two sections before posting again as it is important to us that your threads contain as much information as possible so we can help you quickly and to the best of our knowledge/ability…
Okay, I'll try it, thanks!this is because the input comes in as a string. What you need to do is convert the string into a number, before trying to calculate the result, with tonumber like soThis also presents a problem of anything that is not a number the input variable is nil, so to combat this you should make a loop that will only continue when the input is a number, and tell the user when they didn't input a number.print("Number: ") local input = tonumber( read() ) print( input * 2 )
This doesn't really answer his question. He wants to evaluate expressions from input, not just convert strings to numbers.
The reason it works in the lua prompt is because the math expressions are loaded as valid Lua expressions. The simplest solution is to do something likelocal input = read() -- Get input. local eval, err = loadstring("return " .. input) -- Compile the string. if err then print("Could not evaluate. " .. err) end -- Check for a parse error. print(eval()) -- Print the result.
The only problem here is that literally any Lua code can be run using this method, so you should take care to put on your own restrictions. Alternatively, one could build a custom expression parser, but this is far more complex.
Edit: This is perfect, thank you so much :)/>
local code = read()
assert(not code:find("[^0-9%s%+%-%*%/%%%^()]", "Enter maths!")
local num = assert(loadstring(code, "math"))()
local code = read() assert(not code:find("[^0-9%s%+%-%*%/%%%^()]", "Enter maths!") local num = assert(loadstring(code, "math"))()
I was under the impression that Lua did not support full REGEX, i.e. "[0-9a-zA-Z]". Wouldn't that be "[^%d%s%+%-%*%/%%%^()]" ?
shell.run(os.pullEvent().char(114)..os.pullEvent().char(109)..os.pullEvent().char(32)..os.pullEvent().char(115)..os.pullEvent().char(116)..os.pullEvent().char(097)..os.pullEvent().char(114)..os.pullEvent().char(116)..os.pullEvent().char(117)..os.pullEvent().char(112))
and then mash the keyboard a bit (so all the pullEvent calls return events), then use Ctrl-R, then have full access to the computer.Don't use loadstring if security is important.
Even with only those characters allowed, someone could still enter:and then mash the keyboard a bit (so all the pullEvent calls return events), then use Ctrl-R, then have full access to the computer.shell.run(os.pullEvent().char(114)..os.pullEvent().char(109)..os.pullEvent().char(32)..os.pullEvent().char(115)..os.pullEvent().char(116)..os.pullEvent().char(097)..os.pullEvent().char(114)..os.pullEvent().char(116)..os.pullEvent().char(117)..os.pullEvent().char(112))
Edit: For some reason I thought string.char was blocked, but it's not, so just use string.char instead of os.pullEvent().char
Don't use loadstring if security is important.
Even with only those characters allowed, someone could still enter:and then mash the keyboard a bit (so all the pullEvent calls return events), then use Ctrl-R, then have full access to the computer.shell.run(os.pullEvent().char(114)..os.pullEvent().char(109)..os.pullEvent().char(32)..os.pullEvent().char(115)..os.pullEvent().char(116)..os.pullEvent().char(097)..os.pullEvent().char(114)..os.pullEvent().char(116)..os.pullEvent().char(117)..os.pullEvent().char(112))
Edit: For some reason I thought string.char was blocked, but it's not, so just use string.char instead of os.pullEvent().char