Input
Part of the Fundamentals section of Coddy's Lua journey — lesson 31 of 90.
As of now we stored values that we thought about in variables. Programs usually don't work this way. We receive values from an outer source, a user for example.
To get input from a user or the system we need to write:
var = io.read()This will store the input in the variable var.
The input is always of type string. For example, if the input is 56 then
varwill hold the string"56".
Challenge
BeginnerWrite a program that get input from the user (their name), and then outputs Hello, followed by a space and the user's inputted name.
For example, if the user inputs Bob, the expected output is Hello, Bob.
You will need to:
- Use
io.read()to get input from the user. - Store the input in a variable.
- Print
Hello,and the stored variable in the end (add a space after the comma).
Cheat sheet
To get input from a user:
var = io.read()The input is always stored as a string, even for numbers.
To print a greeting with the input:
name = io.read()
print("Hello, " .. name)Try it yourself
-- Write code hereThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators 2 Relational & Logic
Equality OperatorsRelational OperatorsThe 'and' OperatorThe 'or' OperatorThe 'not' OperatorShort-Circuit EvaluationTruthy and Falsy ValuesRecap - Simple Logic7Basic Conditional Logic
The if-then StatementThe if-then-else StatementThe elseif StatementRecap - Treasure Chest2Variables and Data Types
What is a Variable?NumbersStringsBooleansThe Value 'nil'The type() FunctionNaming ConventionsRecap - Character Profile5Basic Output
Printing LiteralsPrinting VariablesPrinting Multiple ValuesCombining Strings & VariablesThe tostring() FunctionInputCastRecap - Status ReportRecap - Till 1208String Manipulation Basics
string.len()string.upper & string.lowerstring.sub()string.rep()string.find()Recap - Format Username3Operators 1 Arithmetic & Conc
Arithmetic OperatorsModulo OperatorExponentiation OperatorString ConcatenationOperator PrecedenceRecap - Simple Calculations6Project: Character Stats Disp
Welcome MessageDeclare Character Stats9Functions Basics
Declaring a FunctionCalling a FunctionFunctions with ParametersFunctions with Multiple ParamsThe 'return' StatementRecap - Area Calculator