8 Taking Input
Hasan Malik and Rutwa Engineer
Learning Objectives
- Learn the Importance of Taking Input
- Learn to take string inputs
- Learn to take numerical inputs
Introduction
Often, you want your program to use particular pieces of data that are input by the user. To make decisions, output results, and analyse user-entered data, computer programs take in inputs.
In this module, we will learn how Lua takes in string inputs and number inputs (you can’t have boolean inputs—the user entering “true” will be interpreted as a string value!)
String Inputs
Consider this code that uses a string input from the user:
print ("Enter your name: ")
local name = io.read()
print("Your name is" .. " " .. name)
Here, the first line of code is called a prompt. It informs the reader that they are required to input some data.
Then, we create a local variable name. Rather than assigning a value to name by ourselves, we use the built-in io.read()
method to take input from the user.
Let’s say your name is “Matilda”, so you enter “Matilda”. The code will output:
Your name is Matilda
Key Takeaway
Notice how we use string concatenation to add a space between “is” and “Matilda”.
Exercise
Remember the roller-coaster validation program we wrote a while ago (while studying conditional statements—doesn’t that feel like a long time ago!)? It looked something like this:
if ticket and age >=18 and height >= 5 then
print ("You may ride the roller-coaster!")
else
print("You cannot ride the roller-coaster.")
end
Now, try to change it to accommodate user inputs!
Number Inputs
Regardless of which data type we are dealing with, the way Lua takes input from the user is very similar. However, there is one extra step when dealing with numbers.
When Lua takes input, it assumes by default that the user is entering a string. If we want to receive numbers instead, we need to use the tonumber()
function.
Example
Let’s say you want to take the input of a student’s grade (in marks), and output the corresponding letter grade.
print("Enter your grade: ")
local grade = tonumber(io.read())
if grade >= 90 then
print("You get an A+!")
elseif grade >=80 then
print("You get a B!")
elseif grade >=70 then
print("You get a C!")
else
print("You need to improve.")
end
Notice the tonumber()
function.
Let’s say we input 75. The code will automatically check the value of the user’s input with the different cases in the if-elseif-else statements. The code will output:
“You get a C!”
If we input 92, the code will output:
You get an A+!
Now, let’s say we remove the tonumber()
function so that the second line of our code looks like local grade = io.read()
. Now, let’s run the code and input 65 as the grade. What happens?
An error is generated!
This is because Lua, by default, assumes any value entered by the user is a string. Therefore, the code assigns the string value “65” to the local variable grade. Then, as soon as the code enters the if-statement, the code compares the value of grade with 90. However, the computer cannot compare “65” with 90, because Lua cannot compare string values with number values!
Therefore, it is essential to use the tonumber()
function to prevent errors when dealing with numerical user inputs.
Congratulations—you now know how to take input in Lua!