Step 1 of 5 · Reading · ~4 min
Learn
First Steps
Variables and Assignment
A variable in Lua is a name bound to a value. The value carries the type; the name never does. local n = 5 on one line and n = "five" on the next is perfectly legal — Lua is dynamically typed.
The one decision you make on every declaration is whether to write local.
Why does local matter so much?
Leaving local off does not give you a local variable. It creates or overwrites a global: visible to every function in the program and to every library you load.
✓ scoped to this block
local total = 0
✗ global, silently shared with everything
total = 0
Nothing warns you. The bug surfaces much later, when some helper assigns i without local and a loop elsewhere starts skipping values. Globals are also slower — a local lives in a numbered slot inside the running function, while every read of a global is a hash lookup in the globals table.
Rule for this course: write local unless you have a specific reason not to.
The eight types
| Type | Example | Notes |
|---|---|---|
nil | nil | "no value"; also what an unassigned name gives you |
boolean | true, false | exactly these two |
number | 7, 3.5 | integers and floats live under one type |
string | "Ada" | immutable; may hold any bytes |
function | print | a value like any other |
table | {} | the only container Lua has |
userdata | — | memory owned by the host C program |
thread | — | a coroutine |
type(v) reports which one you are holding.
local name = "Ada"
local age = 36
print(type(name))
print(type(age))
print(type(nothingHere))
--> string
--> number
--> nil
That last line is the important one: reading a name that was never assigned is not an error in Lua. You get nil and the program carries on — which is why typos in Lua tend to fail three steps after the actual mistake.
Reading a line of input
io.read() pulls one line from stdin and returns it without the trailing newline. When there is no more input it returns nil.
local name = io.read()
Call it once for each line you intend to consume, and no more. A second call with no second line hands you nil, and the next operation that touches that nil is where your program dies.
Gluing values together
Lua concatenates with .. (two dots), never with +. In Lua, + is arithmetic and only arithmetic.
✓ build one string, print it once
local city = "Lima"
print("Welcome to " .. city .. ", traveller")
--> Welcome to Lima, traveller
✗ + on text
print("Welcome to " + city)
--> script.lua:2: attempt to perform arithmetic on a string value
Lua stamps every runtime error with the file and the line it happened on, and the grader puts the interpreter's own name in front of that. Skip past the noise and read from script.lua: onwards: it is the fastest route to the broken line.
✗ commas, which is the tab trap from Hello, Lua! again
print("Welcome to ", city, ", traveller")
Output (those gaps are TAB characters, not spaces):
Welcome to Lima , traveller
Numbers concatenate happily; Lua converts them for you.
print("age " .. 36)
--> age 36
One spacing quirk: write 36 .. "!" with a space before the dots. 36.. starts to look like a malformed number to the parser. With a variable on the left, name.."!" is fine either way.
Assigning several names at once
The whole right-hand side is evaluated first, then the values are handed out left to right. Surplus names get nil; surplus values are discarded.
local a, b = 1, 2
a, b = b, a
print(a .. " " .. b)
local x, y, z = 10, 20
print(z)
--> 2 1
--> nil
Because the right side is fully evaluated before any assignment lands, a, b = b, a is a genuine swap — no temporary variable, no ordering bug.
Your exercise
Name Card reads a first name and a last name, one per line, and prints them as two lines: first the name in natural order, then the same two names last-name-first, with a comma.
The starter already contains both io.read() calls. Do not read a third time: the tests supply exactly two lines, and a third io.read() returns nil, which dies with attempt to concatenate a nil value the moment you glue it to a string. Build each output line as ONE string with .. and print it once.
The mistake the grader will catch is commas inside print: the gaps become tabs, so Ada Lovelace arrives as Ada, tab, Lovelace and the test fails while your screen looks perfectly fine. Check the spaces too. The second line has a comma and a space after the last name.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…