Reading Errors
Mistakes are a normal part of programming. Even experienced programmers make them every day. What matters is finding and fixing them quickly, and error messages help you with that. Lua has two kinds of errors, and their messages look a little different.
Syntax errors. Before Lua runs a program, it reads all of it and checks that it's written by the rules of the language, its syntax. When it finds a mistake, such as a missing quote, it doesn't run the program at all, not even the lines before the mistake. In this program, a quote is missing on line 2:
print("Good morning, Emma!")
print("Feed the hens)
print("Milk Bella")The output stays empty, and below it, Lua shows this error message in red:
main.lua:2: unfinished string near '"Feed the hens)'Read it from the left:
main.luais the name of the file with the program. On this site, the program in the editor is always calledmain.lua.2is the number of the line with the mistake. You can see the line numbers on the left of the editor.- After the second colon comes a description of the mistake, in English.
unfinished stringmeans a text (a string) that doesn't end.nearsays where Lua noticed the problem: at the text that starts with"Feed the hens). Its closing quote is missing, so the text goes on to the end of the line, and the)became part of it.
The message says where Lua noticed the mistake, and that isn't always where the mistake is. When a parenthesis isn't closed, Lua finds out only on the next line:
print("Good morning, Emma!"
print("Feed the hens")Lua reports:
main.lua:2: ')' expected (to close '(' at line 1) near 'print'')' expected means that Lua expected a ), and (to close '(' at line 1) says which parenthesis it should close: the one on line 1. So when the line in the message looks fine, check the line above it. And when Lua gets to the end of the program while it's still waiting for something, the message ends with near <eof>: eof stands for end of file.
Errors while running. A program without syntax errors starts, and Lua carries out its lines one by one. When it gets to a command that it can't carry out, it stops. What the program printed until then stays in the output, and the error message comes below it:
print("Good morning, Emma!")
print("Feed the hens")
prnt("Milk Bella")
print("Water the carrots")This prints the first two lines, and then comes the red error message:
Good morning, Emma!
Feed the hens
main.lua:3: attempt to call a nil value (global 'prnt')
stack traceback:
main.lua:3: in main chunkThe first line of the message has the same parts as before: the file, the line (3) and the description. attempt to call a nil value (global 'prnt') means that the program tried to call something named prnt, but there is nothing (nil) of that name. Lua knows print, but not prnt. When you see attempt to call a nil value, look for a misspelled name, such as prnt or Print.
Below it, stack traceback: lists where the program was when the error happened, the most recent place first. main.lua:3: in main chunk means line 3 in the main part of your program. Once your programs have functions of their own, this list will get longer and will show which function called which. For now, the first line of the message tells you everything you need. A line of the traceback that starts with [C]: is a part of Lua itself (which is written in the language C), and you can skip it.
Messages you'll meet often:
unfinished string: a missing quote at the end of a text, or two different quotes, as in"Hello'.')' expected: a missing parenthesis, or words without quotes:print(Good morning)gives')' expected near 'morning'.attempt to call a nil value: a misspelled name of a function.attempt to add a 'string' with a 'number':+instead of..when joining a text and a number.
Always fix the first error that Lua reports, and run the program again. A syntax error can hide other mistakes, because nothing runs until it's fixed. And an error while running stops the program, so the lines below it haven't been tried yet.
Instructions
Emma's evening report in the editor has three mistakes. Run the program and read the error message: on which line is the mistake, and what's wrong? Fix it and run the program again. Repeat this until the program prints:
Evening report
Eggs today: 12
Milk: 12 liters
Coins: 60
Tomorrow: rainy
Good night, Rex!Start programming for free
6/7