Skip to content

input

Built-in function · Available everywhere, with no import

input waits for the person running the program to type a line and press Enter, then gives back what they typed.

const name = input("What is your name? ")
print("Hello, #{name}!")

What input gives back is always text. To read a number, convert it: input("How old are you? ").to_int(), or to_int_maybe() to handle something that is not a number.

input(prompt: String = ""): String

Shows prompt, if there is one, without starting a new line, then reads one line. The line comes back without its line break; pressing Enter on its own gives "".

Raises an InputError when there is no more input to read, as when a program reads from a file that has ended, or when the line isn’t valid text. Use input_maybe to handle the first of those instead.

input_maybe(prompt: String = ""): String?

The same as input, but gives back nothing when there is no more input, rather than raising an error. Use it to read until the input runs out:

var total = 0
var line = input_maybe()
while line != nothing {
total += line.to_int_or(0)
line = input_maybe()
}
print(total)