User Input

Variables won't be very useful if we can only set their values to constants inside the program. This would mean we need to change program source every time we want to make calculations with different values!

Luckily, there are ways to ask user to enter values, of course. Reading a single number is what we are going to use the most often, and here how it works:

x = io.read('*n')
print(x, 'squared is', x*x)

This sample program asks user to enter some value and prints that value multiplied by itself (e.g. squared).

The first line makes a call to io.read(...) function. Its only parameter '*n' means that we are going to read a number - so the function will only read characters while they describe some numeric value. (There are more ways to call this function, but we'll return to them later).

When Lua is executed in text-mode (i.e. "console") user will enter data directly in that console. However we use Lua in web-page environment and use slightly changed behavior - when function is called, small prompt dialog will "popup" asking to enter some data.

For exercise please write a tiny program below, which reads two numbers (e.g. x and y) and prints their product (e.g. upon entering 3 and 5 it should answer with 15).

Also it is a good time to start solving main set of problems on this site - try with very first, which simply requires you to input two numbers and print their sum: Sum A+B - hopefully it will be easy!