0

I am making a little program where the user inputs a number and the program generates a random number. But the program just stops right after a user inputs a number. i have no idea what is causing this. hopefully somebody here can help me out with this i am new to lua and programming it's self.

print("Do you want to play a game?")
playerInput = io.read()

if playerInput == "yes" then
    print("What is your number?")
    numGuess = io.read()

    rad = math.random(0,100)

    while numGuess ~= rad do
        if numGuess < rad then
            print("To low")
        elseif numGuess > rad then
            print("to high")
        else 
            print("You got the number")
        end

        print("What is your number?")
        numGuess = io.read()
    end

else
    print("You scared?")
end
Oliver
  • 27,510
  • 9
  • 72
  • 103
carlos
  • 33
  • 1
  • 4
  • 3
    You didn't mention how the program was exiting, I assume it is because you are trying to compare a string with a number. This might help you read the type you want: http://stackoverflow.com/questions/12069109/getting-input-from-the-user-in-lua – Retired Ninja Apr 14 '14 at 03:18
  • On my system it doesn't just stop, it fails with an error message, did it not do that on your system? – Oliver Apr 15 '14 at 01:54

1 Answers1

1

You could try something like this:

-- Seed the random number generator with the current time
-- so the number chosen is not the same every time
math.randomseed(os.time())
rad = math.random(100)
--print("rad = " .. rad)

print("Do you want to play a game?")
playerInput = io.read()

if playerInput == "yes" then
  repeat
    print("What is your number?")
    numGuess = tonumber(io.read())
    if numGuess < rad then
      print("Too low")
    elseif numGuess > rad then
      print("Too high")
    else
      print("You got the number")
    end
  until numGuess == rad
else
  print("You scared?")
end

I added seeding the random number generator, otherwise the number chosen was always 0 for me. I also rearranged your loop a bit to avoid duplication.

I think the main problem you were having is comparing a number to a string, to avoid that I converted the value read to a number using the tonumber function. This will still crash if anything but a number is input so in a real program you'd want to add some error checking.

Here's a version using the while loop rather than repeat and io.read('*n') rather than tonumber(). I moved the prompt to the top of the loop so the body executes after you guess the right number, otherwise the loop would just exit without printing anything because the loop condition was no longer true.

math.randomseed(os.time())
print("Do you want to play a game?")
playerInput = io.read()

if playerInput == "yes" then
    local numGuess = 999
    local rad = math.random(0,100)

    while numGuess ~= rad do
        print("What is your number?")
        numGuess = io.read('*n')

        if numGuess < rad then
            print("To low")
        elseif numGuess > rad then
            print("to high")
        else 
            print("You got the number")
        end
    end
else
    print("You scared?")
end
Retired Ninja
  • 4,785
  • 3
  • 25
  • 35
  • @hjpotter92 The result is the same either way, a number or nil. I went with what I felt was likely to be less confusing, but I did link to another question with an answer that explained '*n' and such above. – Retired Ninja Apr 14 '14 at 05:05
  • @Retired Ninja So just to get this straight io.read() cannot be used to get input for an integer. It has to be put inside of the tonumber() why is that? When i print out the result of numGuess it was still an integer. Also is using repeat better for this type of a situation rather than a while loop? because that is where i was getting the problem of the program ending. – carlos Apr 14 '14 at 15:16
  • I added an additional example keeping the `while` loop and using `io.read('*n')` to read the number. `io.read()` will always return a string unless you use the `'*n'` argument. You can read more about that in the [documentation](http://www.lua.org/pil/21.1.html). – Retired Ninja Apr 14 '14 at 17:41