Summary

In this lesson, you learned that:

  • Lua is a small, fast language from Brazil, made to live inside other programs, so you'll find it in games, editors and databases.
  • A program is a series of commands that Lua carries out one after another, from top to bottom.
  • print("...") prints a line into the output, and print() prints an empty line. Lua treats uppercase and lowercase letters as different.
  • A text (a string) goes in double quotes "..." or in single quotes '...', the same kind at the start and at the end. A word without quotes is a name: print(Carrot) prints nil.
  • Numbers are written without quotes, with a decimal dot. Lua calculates with +, -, *, / and parentheses; * and / come before + and -. / always gives a decimal number: 10 / 2 prints 5.0.
  • .. joins texts and numbers into one text, and it adds no spaces: "Coins: " .. 120. Calculations are done before joining: "Eggs: " .. 7 + 5 gives Eggs: 12. Write spaces around ...
  • print with values separated by commas prints them on one line, with a tab between them.
  • Comments start with -- and last until the end of the line; longer ones go between --[[ and ]]. Lua ignores them, and you can use them to switch code off (comment it out).
  • An error message main.lua:LINE: description says on which line Lua noticed a mistake, and what's wrong. With a syntax error, nothing runs. An error while running stops the program: what it printed stays, and below the message, stack traceback: shows where it happened. Fix the first error first, and run the program again.

In the next lesson, Variables and Types, you'll learn to store values, such as Emma's coins, in variables, and to calculate with them.

Instructions

Before you go on, try it all out on a small task. Emma sold 15 carrots for 5 coins each and 3 pumpkins for 20 coins each. Print her receipt from the market, so that the program prints:

SUNFLOWER FARM
Emma's market day

Carrots: 75 coins
Pumpkins: 60 coins
Total: 135 coins

Don't write the numbers 75, 60 and 135 yourself: let Lua calculate them, and join them with the texts with ...

Above the line that prints the total, write a comment that says what the line calculates.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

7/7