Learn/Lua/3. Reading input and arithmetic
Lesson 3 of 8
Sign in

Lua, lesson 3

Reading input and arithmetic

Standard input (stdin) is the text fed into your program, such as the Input box when you run code here. Lua reads it with io.read.

io.read formats

io.read takes a format that says what to read:

CallReadsGives you
io.read("l")the next line, without the line breaka string
io.read("n")the next number, skipping spaces and new linesa number
io.read("a")everything that is lefta string

io.read() with no argument is the same as . When there is nothing left to read, and return ( also returns if the text is not a number).

2 exercises to try what you just read.

Exercise 1

Pizza split

Press Ctrl Enter or Check to test your code.

Exercise 2

Temperature converter

Press Ctrl Enter or Check to test your code.

io.read("l")
"l"
"n"
nil
"n"
nil

Reading numbers

io.read("n") is the quickest way to get numbers. It skips whitespace, so it works whether the numbers are on separate lines or on the same line separated by spaces:

You can also read several at once: local a, b = io.read("n", "n").

If you already have a line of text, tonumber converts it. It returns nil when the text isn't a number, which you can test for later:

A tricky point: if you mix formats, io.read("n") stops right after the number and leaves the line break unread. A following io.read("l") then returns an empty string. Stick to one style per input when you can.

Arithmetic operators

OperatorMeaningExampleResult
+ - *add, subtract, multiply7 * 214
/division, always a float7 / 23.5
//floor division7 // 23
%remainder7 % 21
^power, always a float2 ^ 101024.0

Lua keeps track of whether a number is an integer or a float. Integer +, -, *, // and % give integers, but / and ^ always give floats. That is why 10 / 2 prints 5.0. When you want a whole-number answer, use //.

// rounds toward minus infinity, so -7 // 2 is -4, and % is defined to match it.

The math library has the rest: math.floor, math.ceil, math.abs, math.max, math.min, math.sqrt, and math.tointeger to turn a float like 5.0 into the integer 5.

Printing decimals neatly

string.format builds a string from a template, like printf in C. %d is an integer, %s a string, %.2f a number with two decimals:

Further reading

  • Lua 5.5 Reference Manual: io.read
  • Lua 5.5 Reference Manual: Arithmetic operators
  • Lua 5.5 Reference Manual: string.format

A group of friends shares a pizza. Read the number of slices on the first line and the number of friends on the second line (always at least 1). Everyone gets the same whole number of slices, and the rest stays in the box. Print:

Sample input:

Sample output:

Read a temperature in Celsius as a line of text (it may have a decimal point) and print it in Fahrenheit with exactly one decimal place. The formula is F = C * 9 / 5 + 32.

Sample input:

Sample output:

Print the Celsius value exactly as it was typed.

local name = io.read("l")
print("Hello, " .. name .. "!")
local a = io.read("n")
local b = io.read("n")
print(a + b)
local price = tonumber(io.read("l"))
print(string.format("Average: %.2f", 10 / 3))   -- Average: 3.33
Each friend gets <n> slices
Leftover: <m>
10
3
Each friend gets 3 slices
Leftover: 1
21.5
21.5 C = 70.7 F