These exercises practice retrieving what the Module 1 reading explained. They aren’t
graded. Work each one on paper or in your head first, then check your answer, and only then run
the code in a %%lua cell or in lua -i to confirm it. The answers are in the collapsed blocks;
open one only after you have an answer of your own.
Worked example: predicting a mixed expression¶
What does print(10 // 3 + 10 % 3 * 2.0) print?
Precedence puts
//,%, and*before+, and the three of them run left to right:(10 // 3) + ((10 % 3) * 2.0).10 // 3is3, an integer, because both operands are integers.10 % 3is1, an integer.1 * 2.0is2.0, a float, because one operand is a float.3 + 2.0is5.0, a float, for the same reason.
So it prints 5.0. A single float operand anywhere in a chain of +, -, and * makes the
result a float from that point on.
Exercise 1.1 (LO-1.2, ★, about 10 minutes)¶
Give the value of each expression. The first is done for you.
-9 // 4is-3: the exact quotient is -2.25, and floor division rounds down to -3.-9 % 49 // -49 % -4
Hint
Use a % b == a - (a // b) * b, with the value of a // b you work out first.
Answer
3, because-9 - (-3) * 4is-9 + 12.-3: the exact quotient is -2.25 again, which rounds down to -3.-3, because9 - (-3) * (-4)is9 - 12. The result has the divisor’s sign.
Exercise 1.2 (LO-1.2, ★★, about 15 minutes)¶
For each expression, give its value and, if it is a number, its subtype. If it raises an error, say so and give the reason.
"5" * "2""5" .. 2"5" == 5"5" < 10#("ab" .. "cd")math.type(10 / 2)tostring(1e15)
Hint
Arithmetic converts strings to numbers, .. converts numbers to strings, and comparisons convert nothing.
Answer
10, an integer: arithmetic converts both strings, and both look like integers."52":..converts the number to a string.false:==never converts, so a string never equals a number.An error,
attempt to compare string with number:<doesn’t convert either.4: the concatenation is"abcd", four bytes."float":/always gives a float, here5.0."1e+15":1e15is a float, and floats print with 14 significant digits, which this one needs an exponent to show.
Exercise 1.3 (LO-1.4, ★★, about 15 minutes)¶
Give the value of each expression, and say for each and or or whether its right-hand side is
evaluated at all.
nil and falsefalse or nil0 or error("unreachable")"" and 1 or 2not not nilnil == false
Answer
nil.nilis falsy, soandreturns it without evaluatingfalse.nil.falseis falsy, soorevaluates and returns its right-hand side.0.0is truthy, soorreturns it and never callserror.1.""is truthy, so"" and 1is1, which is truthy, soorreturns it without evaluating2.false.not nilistrue, andnot trueisfalse. Noandororhere.false. Both are falsy, but they are different values of different types, and==doesn’t convert.
Exercise 1.4 (LO-1.3, ★★, about 15 minutes)¶
Predict both lines this script prints. Then name the one-word change that makes the second line
print unchanged, and explain why describe’s own result is unaffected by the bug.
local function area(width, height)
result = width * height
return result
end
local function describe(width, height)
local result = "room"
local size = area(width, height)
return result .. " of " .. size .. " square meters"
end
result = "unchanged"
print(describe(3, 4))
print(result)Hint
Which result does each function’s assignment or read refer to? Look for the local that declares it, and if there is none, it is the global.
Answer
It prints room of 12 square meters, then 12. area assigns result without local, so it overwrites the global result, which held "unchanged". The fix is local result = width * height in area. describe declares its own local result, which shadows the global inside describe, so area’s assignment to the global doesn’t touch it.
Exercise 1.5 (LO-1.1, ★★, about 15 minutes)¶
For each error message, say whether it is a compile error or a runtime error, which line to look at first, and the most likely cause.
lua: main.lua:2: attempt to concatenate a nil value (local 'name'), with a stack traceback whose next line ismain.lua:4: in main chunklua: report.lua:12: attempt to compare number with stringlua: config.lua:8: 'end' expected (to close 'if' at line 4) near <eof>
Hint
A compile error describes what Lua expected to read. A runtime error describes an operation on values, and runtime errors come with a stack traceback.
Answer
A runtime error. Line 2 concatenates a local
namethat isnil. The traceback says line 4 made the call, so the most likely cause is a call on line 4 that passed no argument, ornil, forname. Look at line 2, then line 4.A runtime error. Line 12 compares a number with a string, probably a value read as text, such as input, that should have gone through
tonumberfirst.A compile error, so nothing in the file ran. The
ifon line 4 has noend. Lua reached the end of the file (<eof>) at line 8 still waiting for it, so line 8 is only where it gave up.
Exercise 1.6 (LO-1.2, ★★★, about 20 minutes)¶
This code splits a duration in seconds into hours, minutes, and seconds:
local t = 3725
local hours = t // 3600
local rest = t % 3600
print(hours, rest // 60, rest % 60)What does it print for
t = 3725?What does it print for
t = -3725?Is the result for
-3725wrong? Check it by computinghours * 3600 + minutes * 60 + seconds.Would the code print the same for
t = 3725.0? If not, what changes?
Hint
For part 2, work out -3725 // 3600 first; floor division rounds down. For part 4, recall which subtype // and % return when an operand is a float.
Answer
1 2 5: one hour, two minutes, and five seconds.-2 57 55.-3725 // 3600rounds -1.03 down to-2, and-3725 % 3600is3475, which splits into 57 minutes and 55 seconds.It isn’t wrong:
-2 * 3600 + 57 * 60 + 55is-3725. Floor division and modulo always recombine into the original number. It is only an unusual way to write a negative duration; to print “-1 h 2 min 5 s”, split the absolute value and add the sign afterward.It prints the same numbers as floats:
1.0 2.0 5.0.//and%give floats when an operand is a float.