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.
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:
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:
84 / 2 --> 42.0Your 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
- Set
secondsto the number of seconds in a day: 24 hours, each of 60 minutes, each of 60 seconds. - Set
costto the price of 3 tickets at 4.50 each, plus a booking fee of 2. - Set
shareto what each person pays when 4 people split a bill of 90. - Set
averageto 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.