This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
moneymaster2012's profile picture

Random Words

Started by moneymaster2012, 05 September 2014 - 06:32 PM
moneymaster2012 #1
Posted 05 September 2014 - 08:32 PM
I'm making a program that uses random words. I'm not talking about "randomly generated words". What I mean is if you type in "test" it will come up as "Hi", or "Hello" with a 50-50 chance.
KingofGamesYami #2
Posted 05 September 2014 - 08:52 PM

local known = {
  test = { "Hi", "Hello" },
}
local input = read()
if known[ input:lower() ] then
  print( known[ input ][ math.random( 1, #known[ input ] ) ] )
else
  print( "I don't understand!" )
end

Does this help?
Edited on 05 September 2014 - 06:52 PM
TurtleHunter #3
Posted 05 September 2014 - 10:07 PM

local known = {
  test = { "Hi", "Hello" },
}
local input = read()
if known[ input:lower() ] then
  print( known[ input ][ math.random( 1, #known[ input ] ) ] )
else
  print( "I don't understand!" )
end

Does this help?

Extra comma in the end of the second line
Dog #4
Posted 05 September 2014 - 10:29 PM
Extra comma in the end of the second line
That comma is fine and can be there or not - it'll work either way. :)/>
Bomb Bloke #5
Posted 06 September 2014 - 03:21 AM
It's a good way of doing it, but one small change I'd make:

local input = read():lower()

It saves having to use :lower() over and over again later in the code (something you forgot to do!).