Reading input: prompt
prompt shows a text, waits until the user types a line and presses Enter, and gives back that line:
name: str = prompt("Enter your name: ")
print("Your name is ${name}")In a terminal, a run looks like this (the user typed Ann):
Enter your name: Ann
Your name is AnnThe text is shown as it is, without a new line after it, so the user types right after it. That’s why it usually ends with a space. prompt() without a text just waits for a line.
Input from a file or another program
A program reads its input from the terminal, or from wherever it is sent:
luma run greet.luma < answers.txt
echo Ann | ./greetEach prompt then reads the next line. The lines don’t appear on screen, because nobody types them, so the text of a prompt and the next output end up on the same line. The examples on this page show runs like that:
name: str = prompt("Enter your name: ")
print("Your name is ${name}")Input:
AnnOutput:
Enter your name: Your name is AnnThe type decides how the line is read
The line is read as the type of the variable it goes into:
age: int = prompt("Age: ")
price: float = prompt("Price: ")
small: byte = prompt("Byte: ")
save: bool = prompt("Save? ")
print(age + 1)
print(price * 2.0)
print(small)
print(save)Input:
41
2.5
200
trueOutput:
Age: Price: Byte: Save? 42
5.0
200
true| Type | What the user may type |
|---|---|
str | anything; the line is kept exactly as typed, spaces included |
int | a whole number: 42, -7, +3, 007 |
byte | a whole number from 0 to 255 |
float | a number like 2.5, -0.25, 3, .5, 1e3, 2.5E-3 |
bool | true or false |
For numbers and bools, spaces and tabs around the text are ignored: 42 is 42. Anything else is invalid input (see below).
The same rule works wherever a value of a known type is expected, for example when adding to a list or a map:
names: [str] = []
names[] = prompt("Name: ")
scores: {str: int} = {}
scores["Ann"] = prompt("Ann's score: ")
print(names)
print(scores)Input:
Bo
17Output:
Name: Ann's score: ["Bo"]
{"Ann": 17}Where no particular type is expected, the line is a str, for example in print(prompt()) or "Hi ${prompt()}". To use a number in a calculation, read it into a variable first: in prompt() + 1, the prompt() is a str.
Waiting for Enter
On its own, prompt waits for a line and throws it away:
prompt("Press Enter to continue...")
print("Let's go!")Input:
Output:
Press Enter to continue...Let's go!Invalid input
If the line doesn’t fit the type, prompt raises error 6. Without a try, the program stops:
age: int = prompt("Age: ")
print(age)Input:
fortyOutput:
Age: example.luma:1:12: runtime error: invalid input "forty": expected an intWith try, the program can ask again. A walk limits how many times:
age: int = 0
err: error = nil
[1, 2, 3].walk(attempt) {
try err {
age = prompt("Age: ")
}
if err == nil { stop }
print("Please type a number.")
}
print(age)Input:
forty
40Output:
Age: Please type a number.
Age: 40End of input
When there is nothing left to read, prompt raises error 7, end of input. This happens at the end of a file sent to the program, or when the user presses Ctrl-D (Linux) or Ctrl-Z then Enter (Windows). A last line that doesn’t end with a new line still counts as a line.
err: error = nil
try err {
line: str = prompt()
}
print(err)Output:
error 7: end of inputDetails
- A line ends at a new line character, which isn’t part of it. A
\rjust before it (Windows line endings) is dropped too. - A
floatis read as the float nearest to the number typed, however many digits it has, exactly like a float written in a program: every float Luma prints reads back as the same float. - A number too large for its type is invalid input:
9223372036854775808for anint,256for abyte,1e999for afloat.
Not available yet
- A default answer, a time limit, or a check function for
prompt(the original Luma had these). Usetryandifinstead.
See Limitations.