lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello, I wrote the following code to require the user to input a number through stdin:

repeat
        print "a number, please:"
        x = io.read "*number"
until x ~= nil and x >= 0 and x < 5

print(x)

If I enter a number outside of the range, it repeats, and asks for another number.  If it is in the range, echoes the number and quits.  If however, I enter anything not a number, it continues in an infinite loop asking for input.  Am I not using io.read "*number" correctly?  Is this behavior the desired behavior of io.read "*number"?  If so, what is the rational here?

I got around this issue by using the following loop instead:

repeat
        print "a number, please:"
        x = tonumber(io.read "*line")
until x ~= nil and x >= 0 and x < 5

print(x)

This code works as expected.  Although it doesn't seem like these two should perform any differently.

--
Zach Dwiel
dwiel.net