Back

Activity 1.4: Asking a Question

divider

Activity 1.4

Asking a Question

Key Concepts

Waiting for the User

Everything Typed Is Text

Three Kinds of Wrong

Back to the Terminal

Today you copy template-console, not template-game.

No window, no love.draw. The file runs top to bottom and stops, like session 1.

Roughly one session in four is like this — the ones where text is genuinely the point.

Two Lines That Ask

io.write("What is your name? ")
local name = io.read()
io.write
Prints without moving to the next line, so the answer is typed right after the question.
io.read
Stops the program until Enter is pressed, then hands back whatever was typed.

The Program Stops and Waits

io.read() does not look ahead and it does not give up. It waits.

A program sitting there doing nothing is usually a program waiting for you to type.

It is not frozen. It is not broken. It wants an answer.

Now the Important Part

You type 16.

Now the Important Part

You type 16.

Lua did not get the number 16. It got the text "16".

Everything typed at a prompt arrives as text, always, with no exceptions.

Three Kinds of Wrong, One Value

io.write("How old are you? ")
local age = io.read()
print("Doubled: " .. age * 2)
print("Is it 16? " .. tostring(age == 16))
print("Type: " .. type(age))

Wrong #1: It Works Anyway

print("Doubled: " .. age * 2)
Terminal window
Doubled: 32

Wrong #1: It Works Anyway

print("Doubled: " .. age * 2)
Terminal window
Doubled: 32

Lua quietly converted the text for you. This is the one that fools people — it proves nothing is wrong, and something is.

Wrong #2: It Lies

print("Is it 16? " .. tostring(age == 16))
Terminal window
Is it 16? false

Wrong #2: It Lies

print("Is it 16? " .. tostring(age == 16))
Terminal window
Is it 16? false

You typed 16 and it says no. The text "16" is not the number 16.

No error. No warning. Just the wrong answer, forever.

Wrong #3: It Crashes

io.write("How old are you? ")
local age = io.read()
if age > 10 then
print("Older than ten.")
end

Wrong #3: It Crashes

io.write("How old are you? ")
local age = io.read()
if age > 10 then
print("Older than ten.")
end
Terminal window
Error: main.lua:4: attempt to compare number with string

This is the friendly one. It tells you.

Same Typed Value, Three Outcomes

age * 2
Works. 32.
age == 16
Silently false.
age > 10
Crashes.

Nothing about your typing changed. Only what the next line did with it.

One Word Fixes All Three

local age = tonumber(io.read())

One Word Fixes All Three

local age = tonumber(io.read())

tonumber takes text and gives back the number it spells.

Do it at the moment you collect the value, not later, not sometimes.

What If They Type a Word?

tonumber("twelve")

You get nil — the same nothing you got from a misspelled variable last session.

It does not crash on the spot. It hands back nothing and lets the next line fail.

Today's Objectives

  • Ask a question and store the answer
  • Say why typed input is always text
  • Show all three ways that goes wrong
  • Convert input with tonumber at the point of collection

Key Terms

io.write
Prints without starting a new line.
io.read
Waits for the user to type a line and press Enter.
String
Text. What input always arrives as.
tonumber
Turns text into the number it spells, or into nil.
nil
Nothing. Not zero, not empty text — no value at all.

'F' → Fullscreen

divider

Build

Task 1: Ask Two Questions

Copy template-console and rename the copy 1-04-greet. Replace main.lua with this:

main.lua
io.write("What is your name? ")
local name = io.read()
io.write("What is your favorite color? ")
local color = io.read()
print("Nice to meet you " .. name .. ".")
print("I like " .. color .. " too.")
Terminal
What is your name? Ada [Enter]
What is your favorite color? blue [Enter]
Nice to meet you Ada.
I like blue too.

Run it and answer both questions. Then change the questions and the sentences to your own, and add a third question.

Notice the space at the end of "What is your name? ". Take it out and your answer is typed with its nose against the question mark.

Task 2: Three Kinds of Wrong

Copy the template again to 1-04-age, and write this:

main.lua
io.write("How old are you? ")
local age = io.read()
print("Doubled: " .. age * 2)
print("Is it 16? " .. tostring(age == 16))
print("Type: " .. type(age))

Run it and type 16:

Terminal
How old are you? 16 [Enter]
Doubled: 32
Is it 16? false
Type: string

Copy all three results onto your sheet. One worked, one was false, and one told you the type was string — even though you typed a number.

Now replace those three lines with a comparison:

main.lua
io.write("How old are you? ")
local age = io.read()
if age > 10 then
print("Older than ten.")
end
Terminal
How old are you? 16 [Enter]
Error: main.lua:4: attempt to compare number with string
main.lua:4: in main chunk

Copy that message onto your sheet too. Three different outcomes, one typed value.

Task 3: Fix It With One Word

Put the three print lines back, and change one line:

main.lua
io.write("How old are you? ")
local age = tonumber(io.read())
print("Doubled: " .. age * 2)
print("Is it 16? " .. tostring(age == 16))
print("Type: " .. type(age))
Terminal
How old are you? 16 [Enter]
Doubled: 32
Is it 16? true
Type: number

Put the comparison back as well and check that it no longer crashes.

Task 4: Use It for Something

Copy the template to 1-04-nextyear and write a program that asks for an age and answers with next year's:

main.lua
io.write("How old are you? ")
local age = tonumber(io.read())
print("Next year you will be " .. age + 1 .. ".")
Terminal
How old are you? 16 [Enter]
Next year you will be 17.

Then add a second question of your own that does arithmetic on the answer — a number of pets, a jersey number, a locker number. Anything you can add to or multiply.

Challenge (Optional): Break It on Purpose

Run 1-04-nextyear again and type twelve instead of a number:

Terminal
How old are you? twelve [Enter]
Error: main.lua:4: attempt to perform arithmetic on local 'age' (a nil value)
main.lua:4: in main chunk

tonumber did not crash. It handed back nil, and line 4 is where somebody finally tried to use it. Write down which line has the mistake and which line reported it.

This is the second time nil has turned up somewhere other than where it was made. It will keep doing that.

divider

Checkpoint

  • 1-04-greet asks three questions in your own wording and answers in sentences
  • All three wrong results from task 2 are written on your sheet
  • 1-04-age now reports number and true
  • 1-04-nextyear does arithmetic on two typed answers
divider

Reflection

Answer the following questions before submitting your work.

  1. Of the three wrong results in task 2, say which one you think is the most dangerous in a real program, and why. The answer is not the one that crashed.
  2. age * 2 gave 32 without tonumber. Explain why that is a problem rather than a convenience.
  3. You typed a word at a number question and the program crashed on a line that looked fine. Describe how you would find the real cause if nobody had told you what you typed.
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete