Jump to content




Random Words

help

  • You cannot reply to this topic
4 replies to this topic

#1 moneymaster2012

  • Members
  • 38 posts

Posted 05 September 2014 - 07: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.

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 05 September 2014 - 07: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 by KingofGamesYami, 05 September 2014 - 07:52 PM.


#3 TurtleHunter

  • Members
  • 112 posts

Posted 05 September 2014 - 09:07 PM

View PostKingofGamesYami, on 05 September 2014 - 07:52 PM, said:

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

#4 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 05 September 2014 - 09:29 PM

View PostTurtleHunter, on 05 September 2014 - 09:07 PM, said:

Extra comma in the end of the second line
That comma is fine and can be there or not - it'll work either way. :)

#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 06 September 2014 - 02: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!).





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users