Learn Lua · First steps · Lesson 1 of 4

Doing sums

A script is a list of instructions that CoCoCo follows from top to bottom. Lua does sums the way you'd write them: + adds, - subtracts, * multiplies and / divides.

LUA
2 + 3   --> 5
6 * 7   --> 42

-- starts a comment: Lua ignores the rest of the line. The lessons use --> to show what a line works out to, so you don't type that part.

As in maths, * and / happen before + and -. Put brackets round the part you want done first:

LUA
2 + 3 * 4     --> 14
(2 + 3) * 4   --> 20

/ always gives a number with a decimal point, even when it divides exactly. 42.0 is the same number as 42:

LUA
84 / 2   --> 42.0

Your code gives back its answers with return. Here the code already returns a set of named answers in braces. Replace the 0 after each name with a sum that works it out.

Steps

  1. Set seconds to the number of seconds in a day: 24 hours, each of 60 minutes, each of 60 seconds.
  2. Set cost to the price of 3 tickets at 4.50 each, plus a booking fee of 2.
  3. Set share to what each person pays when 4 people split a bill of 90.
  4. Set average to the average of 7, 8 and 12: add them up, then divide by how many there are.
Show a hint

Write each sum where its 0 is, and keep the comma at the end of the line. If an answer is too big or too small, check whether Lua did a * or / before a + you wanted first.