What happens is not that the code ends, but that the invitation does not immediately appear on the terminal.
Inside, everything that you post is stored in the I / O buffer by the operating system. Periodically, the buffer is empty and its contents are displayed on the terminal (it is cleared). On most systems, the terminal defaults to line-buffering, which means that every time you write a line-feed character that print() does automatically, it is cleared; on some systems, however, by default it is completely buffered, that is, it is automatically reset only when it is full.
There are two ways to solve this problem. If you want to disable or change the buffering for all the I / O operations in the file (and for this purpose the terminal is calculated as a file), you can use the file:setvbuf() function; in particular, io.output():setvbuf("no") will disable buffering for standard output, which means that everything you write will be displayed immediately. You can also use io.output():setvbuf("line") to enable line buffering on systems where it is supported, but not by default.
Another approach is to manually flush the buffer, which is useful when you want a particular output to be displayed immediately, but you do not want the universal function to disable output buffering. You can do this with the file:flush() function, which immediately flushes the buffer, for example:
-- no newline, so even on line-buffered systems this may not -- display immediately io.write("Enter a number: ") -- so we force it to io.flush()
Note that io.write() and io.flush() are just convenient functions for io.output():write() and io.output():flush() , i.e. they get the current output file, and then call :write() or :flush() on them.
(Why bother with buffering at all? Because it's faster - writing data to a terminal or file is expensive, and writing one big thing is faster than writing a lot of little things. In most cases, it doesn't matter, things are not being written at the moment the code starts , so the OS saves the data that should be written to the buffer, and then makes one big record when the buffer is full.)