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. Not +. In Lua, + is arithmetic and only arithmetic.
✓ build one string, print it once
local name = "Ada"
print("Hello, " .. name .. "!")
--> Hello, Ada!
✗ + on text
print("Hello, " + name)
--> 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 — this is the tab trap again
print("Hello, ", name, "!")
Output (those gaps are TAB characters, not spaces):
Hello, Ada !
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
Personal Greeting reads one name and prints Hello, <name>!.
The starter already contains local name = io.read(). Do not read again — the tests supply exactly one line, so a second io.read() returns nil and the concatenation dies with attempt to concatenate a nil value.
Build a single string with .. and print it once. The mistake the grader will catch is print("Hello, ", name, "!"): the commas insert tab characters, so Hello, Ada! arrives as Hello, tab Ada tab ! and the test fails while your screen looks perfectly fine.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…