function IsAnInt(input) result = tonumber(input) ~= nil return result end function GetNumericInput(prompt) while true do print(prompt .. ":") ans = io.read() if IsAnInt(ans) then return tonumber(ans) end print("Your entry was not a number.") end end function isempty(string) return string == nil or string == '' end function GetInput(question) while true do print(question .. "?") ans = io.read() if isempty(ans) then print("Your entry seems empty.") else return ans end end end function AskToPlayAgain() while true do ans = string.upper(GetInput("Would you like to play again? (\"YES\" or \"NO\")")) if ans == "YES" or ans == "Y" then return true end if ans == "N" or ans == "NO" then return false end print("Please respond with \"YES\" or \"NO\"") end end Running = true GotMaxNumber = false GameOver = false MaxNumber = 0 Answer = 0 Guess = 0 Tries = 0 print("*** GUESS A NUMBER : LUA ***") math.randomseed(os.time()) while Running == true do while GotMaxNumber == false do MaxNumber = GetNumericInput("Please enter the maximum number you wish to use") if MaxNumber > 0 then GotMaxNumber = true Answer = math.random(1,MaxNumber) GameOver = false Tries = 0 print("Ok, I am thinking of a number between 1 and " .. MaxNumber) else print("The maximum number should be greater than zero.") end end Guess = GetNumericInput("Guess a number between 1 and " .. MaxNumber) Tries = Tries + 1 if Guess > Answer then print("Too High.") end if Guess < Answer then print("Too Low.") end if Guess == Answer then if Tries == 1 then print ("You got it in 1 try!") else print ("You got it in " .. Tries .. " Tries!") end GameOver = true end if GameOver == true then Running = AskToPlayAgain() if Running == true then GotMaxNumber = false end end end print("Thanks for playing!") print("*** GAME OVER ***")