'F' → Fullscreen
No window today — everything here is easier to see as text. Copy template-console, rename it 1-19-lists, and work in main.lua.
local scores = {90, 85, 78}
print("There are " .. #scores .. " scores")print("The last one is " .. scores[#scores])There are 3 scoresThe last one is 78scores[#scores] looks strange and reads simply: the element at the position "how many there are." Add a fourth score to the table and run it again without changing anything else. Both lines follow along.
First, make it fail. Type this and run it:
local scores = {90, 85, 78}local fourth = scores[4]
print("Fourth score: " .. fourth)Error: main.lua:4: attempt to concatenate local 'fourth' (a nil value)
main.lua:4: in main chunkWrite down which line the error names, and which line the mistake is on. They are not the same line, and that is the point of the whole session.
Now fix it, by checking before you use it:
local scores = {90, 85, 78}local wanted = 4
if scores[wanted] == nil then print("There is no score number " .. wanted)else print("Score number " .. wanted .. " is " .. scores[wanted])endThere is no score number 4Change wanted to 2 and run it again. The same program now takes the other branch.
Predict first, on your sheet. Then run it.
local scores = {90, 85, 78}local backup = scores
backup[1] = 0
print("backup[1] is " .. backup[1])print("scores[1] is " .. scores[1])Write down what you expected and what happened. If you were surprised, say so — that is worth marks and it is worth more than being right.
One table cannot become two by giving it another name. To get a real copy you have to move the elements across one at a time:
local scores = {90, 85, 78}local copy = {}
for i = 1, #scores do copy[i] = scores[i]end
copy[1] = 0
print("copy[1] is " .. copy[1])print("scores[1] is " .. scores[1])copy[1] is 0scores[1] is 90scores[1] is still 90. This time there really are two tables, and the loop is what made the second one.
Write a program that asks for a score number with io.read(), and answers sensibly for any number typed — including 0, including 99, including a word.
Remember what session 4 taught you about what io.read() hands back, and what tonumber does with something that is not a number.
Last session the exam agreed with Lua. This session it disagrees twice, and these two are the ones most likely to cost you points.
local scores = {90, 85, 78}local backup = scoresprint(#scores)print(scores[4])scores ← [90, 85, 78]backup ← scoresDISPLAY(LENGTH(scores))DISPLAY(scores[4])The reference sheet says aList ← bList assigns a copy of the list. So on the exam, backup ← scores gives you two separate lists, and changing one leaves the other alone.
In Lua it does not. You proved that in task 3.
The sheet is explicit: for any list operation, an index below 1 or above the length produces an error message and the program terminates. There is no nil, and nothing after that line runs.
In Lua you get nil and the program keeps going — which is why task 2's error pointed at the wrong line.
| Lua | Exam notation | |
|---|---|---|
| Number of elements | #scores | LENGTH(scores) |
backup after assignment | the same list | a copy |
scores[4] on a 3-element list | nil, keeps running | error, program ends |
scores[1] still being 90Answer the following questions before submitting your work.
nil.backup and scores changed too. Explain what local backup = scores actually did.Submit the required files to the appropriate dropbox.