11. Variables and types — Homework solutions

The .lua solution files are in exercises/11/homework/solutions/. The walkthroughs below explain the thinking.

Problem 1 — Player vitals

Problem. Declare four local variables of the right types and print each with a label.

How to think about it. Four declarations, four print lines. Match types to values: a string for name, whole numbers for level and hit_points, a boolean for alive. Each label is a plain string, joined with .. or passed as a second argument.

Worked solution.

local name = "Keiko"
local level = 7
local hit_points = 95
local alive = true

print("Name: " .. name)
print("Level: " .. level)
print("Hit points: " .. hit_points)
print("Alive: " .. tostring(alive))

The last line uses tostring(alive) because Lua won't glue a boolean onto a string with ..; tostring makes it "true" or "false". The comma form prints booleans directly instead:

print("Alive:", alive)

Common mistakes.

  • Leaving local off the declaration. The print still works (silent bug), but level leaks into a global. Keep local.
  • Quoting true or false. That makes them strings, so the next problem's type() test reports string.

Problem 2 — Type checker

Problem. Print type() of five values.

How to think about it. Five print calls, each wrapping type() around a literal; no variable needed.

Worked solution.

print(type("world"))
print(type(42))
print(type(3.14))
print(type(false))
print(type(nil))

The output is:

string
number
number
boolean
nil

Common mistakes.

  • Wrapping the call in quotes: print("type(42)"). That prints the literal text type(42). Without quotes, Lua runs it and prints the type.

Problem 3 — Rename and reassign

Problem. Rename three poorly named variables, print, reassign, print again.

How to think about it. This is about reading code. The starter values hint at each variable's meaning: a = "Sword of Light" is an item name, so rename it item_name. Then reassign (no local) and print.

Worked solution. Given a starter:

local a = "Sword of Light"
local b = 12
local c = true

A reasonable rewrite:

local item_name = "Sword of Light"
local item_level = 12
local is_equipped = true

print("Before:", item_name, item_level, is_equipped)

item_name = "Shield of Ages"
item_level = 5
is_equipped = false

print("After: ", item_name, item_level, is_equipped)

Common mistakes.

  • Adding local to the reassignment lines. That makes new variables shadowing the originals, rarely what you meant.
  • Naming a variable after a reserved word like end or function. Lua refuses, with the error pointing at the name.

Challenge — The nil mystery

Problem. Declare a variable with no value, confirm it is nil, then assign a value and confirm it no longer is.

How to think about it. local x (no =) is a valid declaration: the variable exists, its value nil. Print type(x), set x = "something", then print type(x) again.

Worked solution.

--[[
  nil is Lua's word for "there is no value here at all".
  It is not the same as 0 or "" or false. It is the absence
  of a value. A variable that has been declared but never
  assigned holds nil.
]]

local treasure
print("Before assignment:", type(treasure))   -- nil

treasure = "Gold Coin"
print("After assignment: ", type(treasure))   -- string

Common mistakes.

  • Assigning nil explicitly with local treasure = nil. That works, but it is extra typing; a bare declaration is already nil.
  • Thinking nil is the same as false. They are different types. The related but separate idea of "truthy and falsy" comes in Chapter 16 on if.

Done?

Next — Strings — digs into the type you have used most. Then numbers, keyboard input, and the Part 3 mini-project.