15 posts
Location
Canada
Posted 29 January 2013 - 07:44 PM
I have been lurking these forums for quite some time, and have been able to find what i need thus far without posting. but when it comes to the math aspect or tables aspect of Lua/CC i get completely lost.
I am trying to create a program that requires you to input a multiple of "X"
for the sake of argument we will say x = 4.
What i need to know, is how i can make the computer check to make sure the number the user inputs is a multiple of 4. that way i can create an "if" statement based off the result. (if it is a multiple of 4, move on, otherwise toss an error message to the user)
so like, an input of 4, 8, 12,16, 20, …2084 will all allow the program to progress, where as anything else will return with a bad input warning, slight pause, and then re ask the question.
and with that, a preemptive thanks for taking the time to help me out :)/>/>
8543 posts
Posted 30 January 2013 - 03:40 AM
Split into new topic.
7508 posts
Location
Australia
Posted 30 January 2013 - 03:41 AM
To check if a number is a multiple of another number we can use the modulus/modulo operator
x % 4
this will give us the remainder of x divided by 4, meaning that if x was a multiple of 4 it would be 0, but if it was say 5 it would be 1
using this knowledge we can do the following
local num
while true do
write("Input a multiple of 4: ")
num = tonumber(read())
if not num then
print("Invalid input, not a number")
elseif num % 4 == 0 then
break
else
print("Invalid input, not divisible by 4")
end
end
2088 posts
Location
South Africa
Posted 30 January 2013 - 03:45 AM
Yes, this is definantely possible! You can achieve this in two different ways (they are the same thing, but you can write them out
differently to get an answer). It is called
Modulus or
math.fmod( x, y )What it basically does is return the remainder of the sum : x / y
Using modulus:
x = 100
y = 5
if x % y == 0 then -- if the remainer of x / y is 0 (if there isn't a remainder at all then...
-- if it is
end
So you can try this for example using variadic arguments.
args = {...}
number = tonumber(args[1])
multiple = tonumber(args[2])
if not number or not multiple or #args ~= 2 then
print("Usage: " .. fs.getName(shell.getRunningProgram()) .. " <number> <multiple>")
return
end
term.clear()
term.setCursorPos(1, 1)
print("So you don't know if the number " .. multiple .. " is a multiple of " .. number .. "? Well I'll check for you!\n")
sleep(1)
print(number % multiple == 0 and ("Yes! " .. multiple .. " is a multple of " .. number .. "!\n\n" .. number .. "/" .. multiple .. " = " .. number/multiple) or ("No! " .. multiple .. " is not a multple of " .. number .. "! Don't be silly!"))
That way of printing is basically using an if statement within it to only using one line. It is the same as this:
if number % multiple == 0 then
print("Yes! " .. multiple .. " is a multple of " .. number .. "!\n\n" .. number .. "/" .. multiple .. " = " .. number/multiple)
else
print("No! " .. multiple .. " is not a multple of " .. number .. "! Don't be silly!")
end
</multiple></number>
252 posts
Location
The Netherlands
Posted 30 January 2013 - 07:06 AM
There is also a "harder" way of doing this.
You could first divide 'multiple' by 4 and then check if the result is a round number, in other words, check if the result is equal to math.floor( result ).
For example:
local multiple = tonumber( read() )
local x = 4
if ( math.floor( multiple / 4 ) == multiple / 4 ) then
print("That's right! "..multiple.." is a multiple of "..x.."!")
else
print("D'oh! "..multiple.." is not a multiple of "..x.."!")
end
This, however, is the longest and hardest way. I wouldn't recommend using it, use the modulo instead.
EDIT: I suddenly realised how useless this code is. Please forget about it. Like I said, use the modulo.
5 posts
Location
France
Posted 30 January 2013 - 07:26 AM
To make a condition necessary you can use the assert function, in this case it will be:
assert (x%y==0,x.." is not a "..y.." multiple")
this line will stop the program and print "x is not a y multiple" if it is the case, and will do nothing in the other case.
309 posts
Location
Sliding between sunbeams
Posted 30 January 2013 - 07:35 AM
snip
EDIT: I suddenly realised how useless this code is. Please forget about it. Like I said, use the modulo.
Not quite useless, if you replace the 4 by x.
392 posts
Location
Christchurch, New Zealand
Posted 30 January 2013 - 08:54 AM
I like the part where TheOriginalBIT ninja'd everyone :ph34r:/>
15 posts
Location
Canada
Posted 30 January 2013 - 08:58 AM
cool, thanks for the (quite fast i will note) help everyone.
this works just how i need it. time to dive back into the code :D/>
2088 posts
Location
South Africa
Posted 30 January 2013 - 09:32 AM
I like the part where TheOriginalBIT ninja'd everyone :ph34r:/>
I had the tab open when he had posted it already xD Also I added more information :D/>
15 posts
Location
Canada
Posted 30 January 2013 - 10:05 AM
so i have one more quick question. >.<
using the code that TheOriginalBIT posted, is there a way I can have it also know if a key word is used and act on that?
This "multiple of X" code is for the data gathering aspect of a turtle program I am making. And I have thus far, had in every question the option to type "quit" at any time to abort the program.
basically is there a way to put somewhere inside the "this is not a number" code to check it against the word quit before throwing the "invalid input"?
sorry for dragging the thread on ^.^' im really not familiar with any of the math related code whatsoever. Lua is the first language I have had even a hint of a clue in, and its been a slow learning process for me lol.
8543 posts
Posted 30 January 2013 - 11:27 AM
Instead of directly tonumber()ing the results of the read(), do this:
input = read()
num = tonumber(input)
The rest of the code is the same. In the part where it says it's an invalid number add,
if input == "quit" then
--whatever
end
7508 posts
Location
Australia
Posted 30 January 2013 - 11:32 AM
thats ok. yeh you can. remove the
num = tonumber(read())
and replace it with
input = read()
num = tonumber(input)
this then gets the input from the user, without checking its a number first. you can then just do something like this
if input:lower() == "yes" then
-- they typed yes, or YES, or Yes, or yEs, or yeS, etc.........
elseif not num then
-- everything else beyond here is the same.
end
EDIT: Damn ninja'd :ph34r:/>
15 posts
Location
Canada
Posted 30 January 2013 - 04:10 PM
thanks again for the help :D/>
everything is working like a charm now.
1619 posts
Posted 30 January 2013 - 04:21 PM
There is also a "harder" way of doing this.
You could first divide 'multiple' by 4 and then check if the result is a round number, in other words, check if the result is equal to math.floor( result ).
For example:
local multiple = tonumber( read() )
local x = 4
if ( math.floor( multiple / 4 ) == multiple / 4 ) then
print("That's right! "..multiple.." is a multiple of "..x.."!")
else
print("D'oh! "..multiple.." is not a multiple of "..x.."!")
end
This, however, is the longest and hardest way. I wouldn't recommend using it, use the modulo instead.
EDIT: I suddenly realised how useless this code is. Please forget about it. Like I said, use the modulo.
How the hell do you have an image in your signature?!
7508 posts
Location
Australia
Posted 30 January 2013 - 04:31 PM
How the hell do you have an image in your signature?!
They didn't remove existing signatures.
8543 posts
Posted 30 January 2013 - 07:37 PM
There is also a "harder" way of doing this.
You could first divide 'multiple' by 4 and then check if the result is a round number, in other words, check if the result is equal to math.floor( result ).
For example:
local multiple = tonumber( read() )
local x = 4
if ( math.floor( multiple / 4 ) == multiple / 4 ) then
print("That's right! "..multiple.." is a multiple of "..x.."!")
else
print("D'oh! "..multiple.." is not a multiple of "..x.."!")
end
This, however, is the longest and hardest way. I wouldn't recommend using it, use the modulo instead.
EDIT: I suddenly realised how useless this code is. Please forget about it. Like I said, use the modulo.
How the hell do you have an image in your signature?!
He doesn't. :)/>